ACE_Message_Block内存池
请问一下有没有基于ACE_Message_Block的内存池的实现代码。。每个Msg里面的ACE_Data_Block都是固定大小的。 需要改变Message_Block的分配器 搞定了。。把代码粘出来。希望对大家有用。。#ifndef _ACE_MESSAGE_BLOCK_MANAGER
#define _ACE_MESSAGE_BLOCK_MANAGER
#include "Config.h"
class CMsgMgr
{
public:
CMsgMgr() {};
virtual ~CMsgMgr(){};
public:
void Init( int msgsize = 1024, int msgnum = 2048 );
void Release();
ACE_Message_Block * AllocMsg( void );
private:
ACE_Recursive_Thread_Mutex _lock;
ACE_Allocator * msg_allocator_;
ACE_Allocator * data_allocator_;
ACE_Allocator * buff_allocator_;
};
ACE_Message_Block * CMsgMgr::AllocMsg( void )
{
ACE_Message_Block *msg;
ACE_NEW_MALLOC_RETURN( msg,
static_cast<ACE_Message_Block*>( msg_allocator_->malloc(sizeof( ACE_Message_Block ) ) ),
ACE_Message_Block( 1024,0,0,0,buff_allocator_,0,0,ACE_Time_Value::zero,ACE_Time_Value::max_time,data_allocator_,msg_allocator_ ),
0
);
msg->reset();
return msg;
}
void CMsgMgr::Init(int msgsize /* = 1024 */, int msgnum /* = 2048*/)
{
typedef char BUFFCOUNT;
msg_allocator_=new ACE_Cached_Allocator<ACE_Message_Block,ACE_SYNCH_MUTEX>(msgnum);
data_allocator_ =new ACE_Cached_Allocator<ACE_Data_Block,ACE_SYNCH_MUTEX>(msgnum*2);
buff_allocator_ = new ACE_Cached_Allocator<BUFFCOUNT,ACE_SYNCH_MUTEX>(msgnum*2);
}
void CMsgMgr::Release()
{
delete msg_allocator_;
delete data_allocator_;
delete buff_allocator_;
}
typedef ACE_Singleton<CMsgMgr, ACE_Null_Mutex> MsgMgr;
#endif
初始化
MsgMgr::instance()->Init();
用的时候只要这样子。
ACE_Message_Block* pMsg = MsgMgr::instance()->AllocMsg();
用完
pMsg->release();
结束
MsgMgr::instance()->release();
页:
[1]