基于STL封装的内存池类,已经在linux下做过稳定性测试。 在音视频开发中经常要用到缓存队列,于是封装了此类。有需要的可以直接到这下载: http://download.csdn.net/detail/mtour/8021749
#ifndef MEMPOOL.H
#define MEMPOOL.H
#include <pthread.h>
#include <stdio.h>
#include <list>
#include <iostream>
using namespace std;
// when you have used the mem, call InsertFreeItem
#define DEFAULT_MEM_COUNT 32
template <class TYPE>
class CMemPool
{
public:
CMemPool(void);
~CMemPool(void);
int Init(int memCount=DEFAULT_MEM_COUNT);
void Fini();
int GetFreeMemCount(){return m_nFreeMemItemCount;};
TYPE* GetFreeMem();
void InsertFreeMem(TYPE* pItem);
int GetUseMemCount(){return m_nUseMemItemCount;};
TYPE* GetUsedMem();
void InsertUsedMem(TYPE* pItem);
private:
int m_bInit;
int m_nMemSize;
int m_nMemItemCount;
int m_nFreeMemItemCount;
int m_nUseMemItemCount;
pthread_mutex_t m_freeListCs;
list<TYPE*> m_freeMemList;
pthread_mutex_t m_useListCs;
list<TYPE*> m_useMemList;
};
// implention
template <class TYPE>
CMemPool<TYPE>::CMemPool(void)
{
m_bInit=0;
m_nMemItemCount=DEFAULT_MEM_COUNT;
m_nFreeMemItemCount=m_nMemItemCount;
m_nUseMemItemCount=0;
pthread_mutex_init(&m_freeListCs,0);
pthread_mutex_init(&m_useListCs,0);
}
template <class TYPE>
CMemPool<TYPE>::~CMemPool(void)
{
pthread_mutex_destroy(&m_freeListCs);
pthread_mutex_destroy(&m_useListCs);
}
template <class TYPE>
int CMemPool<TYPE>::Init(int memCount)
{
if (m_bInit)
{
return 0;
}
if (memCount<=0)
{
return -1;
}
for (int i = 0; i<memCount; i++)
{
TYPE* pMemItem=new TYPE;
if (0!=pMemItem)
{
pthread_mutex_lock(&m_freeListCs);
m_freeMemList.push_back(pMemItem);
pthread_mutex_unlock(&m_freeListCs);
}
else
{
return -1;
}
}
m_nMemItemCount=memCount;
m_nFreeMemItemCount=memCount;
m_nUseMemItemCount=0;
m_bInit=1;
return 0;
}
template <class TYPE>
void CMemPool<TYPE>::Fini()
{
if (!m_bInit)
{
return;
}
pthread_mutex_lock(&m_freeListCs);
for (class list<TYPE *>::iterator itor = m_freeMemList.begin(); itor!=m_freeMemList.end(); itor++)
{
TYPE* pItem=*itor;
delete pItem;
itor = m_freeMemList.erase(itor);
}
pthread_mutex_unlock(&m_freeListCs);
pthread_mutex_lock(&m_useListCs);
for (typename list<TYPE*>::iterator itor = m_useMemList.begin(); itor!=m_useMemList.end(); itor++)
{
TYPE* pItem=*itor;
delete pItem;
itor = m_useMemList.erase(itor);
}
pthread_mutex_unlock(&m_useListCs);
m_nMemItemCount=DEFAULT_MEM_COUNT;
m_nFreeMemItemCount=m_nMemItemCount;
m_nUseMemItemCount=0;
m_bInit=0;
}
template <class TYPE>
TYPE* CMemPool<TYPE>::GetFreeMem()
{
pthread_mutex_lock(&m_freeListCs);
int nListSize=m_freeMemList.size();
if (nListSize>0)
{
TYPE* pItem=m_freeMemList.front();
m_freeMemList.pop_front();
m_nFreeMemItemCount--;
pthread_mutex_unlock(&m_freeListCs);
return pItem;
}
pthread_mutex_unlock(&m_freeListCs);
return NULL;
}
template <class TYPE>
void CMemPool<TYPE>::InsertFreeMem(TYPE* pItem)
{
if (NULL==pItem)
{
return;
}
pthread_mutex_lock(&m_freeListCs);
m_freeMemList.push_back(pItem);
m_nFreeMemItemCount++;
pthread_mutex_unlock(&m_freeListCs);
}
template <class TYPE>
TYPE* CMemPool<TYPE>::GetUsedMem()
{
pthread_mutex_lock(&m_useListCs);
int nListSize=m_useMemList.size();
if (nListSize>0)
{
TYPE* pItem=m_useMemList.front();
m_useMemList.pop_front();
m_nUseMemItemCount--;
pthread_mutex_unlock(&m_useListCs);
return pItem;
}
pthread_mutex_unlock(&m_useListCs);
return NULL;
}
template <class TYPE>
void CMemPool<TYPE>::InsertUsedMem(TYPE* pItem)
{
if (NULL==pItem)
{
return;
}
pthread_mutex_lock(&m_useListCs);
m_useMemList.push_back(pItem);
m_nUseMemItemCount++;
pthread_mutex_unlock(&m_useListCs);
}
#endif /* MEMPOOL.H */
测试程序:
#include "MemPool.h"
typedef struct FRAME_ITEM
{
int nBufLen;
char* pBuf;
FRAME_ITEM()
{
pBuf=new char[32*1024];
nBufLen=32*1024;
}
~FRAME_ITEM()
{
if (0!=pBuf)
{
delete pBuf;
}
}
}FRAME_ITEM;
int main()
{
CMemPool<FRAME_ITEM> pool;
int nRet=pool.Init();
if (nRet!=0)
{
printf("init failed \n");
return 0;
}
int n=100;
while (n)
{
FRAME_ITEM* pItem=pool.GetFreeMem();
printf("free item num %d \n",pool.GetFreeMemCount());
pool.InsertUsedMem(pItem);
printf("used item num %d \n",pool.GetUseMemCount());
pItem=pool.GetUsedMem();
pool.InsertFreeMem(pItem);
usleep(10*1000);
n--;
}
pool.Fini();
printf("free item num %d \n",pool.GetFreeMemCount());
getchar();
return 0;
}
在使用过程中如有疑问请留言