/*
* MemoryPool.cpp
*
* Created on: 2009-7-14
* Author: yul
*/
#include "MemoryPool.h"
#include <numeric>
MemoryPool::MemoryPool(IAllocator *Allocator,
DWORD dwChunkSize_,
DWORD dwIncrease_ ,
DWORD dwGap_
) : m_dwChunkSize(dwChunkSize_),
m_dwIncrease(dwIncrease_),
m_dwExpiredSecond(120),
m_pAllocator(Allocator)
//,ThreadPool(GetThreadPool< typename shared_ptr<LOKI_THREAD_POOL_TYPE> >())
{
//GetThreadPool< typename smart_pool<LOKI_THREAD_POOL_TYPE> >()
//ThreadPool->size_controller().resize(LOKI_THREAD_POOL_NUM);
//TRACE("%s\n",typeid(ThreadModel).name());
m_dwGap = 1;
while((dwGap_>>=1))
{
m_dwGap <<= 1;
}
#if 0
//Not Lazy first Preallocate
MemoryTrunk memoryTrunk = Preallocate(dwChunkSize);
assert(memoryTrunk.lpVoid);
FreeTable.insert(memoryTrunk);
#endif
}
void MemoryPool::SetAllocatorPolicy(IAllocator *Allocator)
{
boost::recursive_mutex::scoped_lock lock(m_Mutex);
//Lock Lock (this);
m_pAllocator = Allocator;
}
MemoryPool::~MemoryPool()
{
//ThreadPool->empty();
//ClearThreadPool< typename smart_pool<LOKI_THREAD_POOL_TYPE> >();
{
boost::recursive_mutex::scoped_lock lock(m_Mutex);
//Lock Lock (this);
// cout<< __FUNCTION__<<"Total Memory:"<< (GetFreeMemSize()>>10)<<"Allocate Memory:"<<(GetAllocMemSize()>>10)<<endl;
MemoryTable::nth_index<0>::type &idx=get<0>(AllocateTable);
for( nth_index_iterator<MemoryTable,0>::type it = idx.begin();it!= idx.end();it++ /*it = idx.begin()*/)
{
//MemoryTrunk memoryTrunk;
MemoryTrunk memoryTrunk = *it;
#if 0
Free(memoryTrunk.lpVoid);
#else
FreeTable.insert(memoryTrunk);
#endif
}
AllocateTable.clear();
{
//合并空闲内存块
//Stop to Schedule FreeRun
freeCache.clear();
//Stop to Schedule ClearExpire
m_dwExpiredSecond = 0;
(*LogMessage)<< log4cpp::Priority::INFO
<<__FUNCTION__<<"Before Trim";
size_t size = Trim();
if ( size )
{
//nth_index<MemoryTable,3>::type &idxFree3=get<3>(FreeTable);
//std::copy ( idxFree3.begin(), idxFree3.end() ,
// std::ostream_iterator<long *> ( std::cout, " ") );
//cout<< __FUNCTION__<<"Trim "<<size<<"Total "<<FreeTable.size()<<" Chunk"<<"Total Memory:"<< (GetFreeMemSize()>>10)<<"Allocate Memory:"<<(GetAllocMemSize()>>10)<<endl;
// (*LogMessage)<< log4cpp::Priority::INFO<<__FUNCTION__<<"Trim "<<size<<"Total "<<FreeTable.size()<<" Chunk"<<"Total Memory:"<< (GetFreeMemSize()>>10)<<"Allocate Memory:"<<(GetAllocMemSize()>>10);
}
(*LogMessage)<< log4cpp::Priority::INFO
<<__FUNCTION__<<"After Trim";
}
}
//if(!GlobalPolicy<>::SingletonPolicy<typename smart_pool<lifo_pool> >::BeDestroyed())
// ThreadPool->join(GetTimeStamp(5));
{
boost::recursive_mutex::scoped_lock lock(m_Mutex);
//Lock Lock (this);
//最后释放内存块
//TRACE("End\n");
nth_index<MemoryTable,0>::type &idxFree=get<0>(FreeTable);
for( nth_index_iterator<MemoryTable,0>::type it = idxFree.begin();it!= idxFree.end();it++ /*= idxFree.begin()*/)
{
MemoryTrunk memoryTrunk = *it;
// (*LogMessage)<< log4cpp::Priority::INFO<<__FUNCTION__<<" Deallocate Trunk "<<memoryTrunk.m_RawChunkID;
// cout<<"Deallocate Trunk"<<memoryTrunk.ulID<<endl;
/* TRACE("Deallocate Trunk [%d] 0x%lx size %d K Raw Chunk Size %d K\n",
memoryTrunk.ulID,
memoryTrunk.m_lpVoid,
memoryTrunk.m_dwChunkSize>>10,
memoryTrunk.m_dwRawChunkSize>>10
);*/
//AllocFactory::Instance(AllocatorID).
//memoryTrunk.m_pAllocator->deallocate(memoryTrunk.lpVoid,memoryTrunk.dwChunkSize);
memoryTrunk.Deallocate();
}
FreeTable.clear();
//MemoryTable_Allocator.ChunkCache.clear();
//MemoryTable_Allocator.get_allocator().purge_memory();
//MemoryTable_Allocator::purge_memory();
//boost::pool<Allocator>::purge_memory();
//ThreadPool->empty();
ThreadPoolSingleton<>::GetInstance().size_controller().resize(0);
ThreadPoolSingleton<>::GetInstance().clear();
}
//ThreadPool->empty();
}
LPVOID MemoryPool::Malloc(DWORD dwUseSize,DWORD dwGap_ )
{
//Profile(PerformanceTest);
if (dwUseSize==0)
return NULL;
/* DWORD dwThread = GetCurrentThreadId();
if ( IsThreadReEnter(dwThread) && !ThreadIDCache.count(dwThread))
{
HANDLE hThread = OpenThread(THREAD_TERMINATE |THREAD_QUERY_INFORMATION,TRUE ,dwThread);
if (hThread )
{
//if (!AcquireThreadHandleSet.count(hThread))
Lock Lock (this);
AcquireThreadHandleSet.insert(hThread);
ThreadIDCache[dwThread] = hThread;
}
}
*/
boost::recursive_mutex::scoped_lock lock(m_Mutex);
//Lock Lock (this);
//Lazy first Preallocate
static bool bInitOnce = false;
//boost::function<void (typename thisType)> func = InitOnce;
if(!bInitOnce)
bInitOnce = (bInitOnce ? bInitOnce:RunNtimes<1>(boost::bind(&MemoryPool::InitOnce,this)));
DWORD dwChunkSize;
if (dwGap_ && dwGap_ != m_dwGap)
{
DWORD dwResult = 1;
while((dwGap_>>=1))
{
dwResult <<= 1;
}
dwGap_ = dwResult;
}
if(!dwGap_)
dwChunkSize = dwUseSize;
else
dwChunkSize = (( dwUseSize + dwGap_ )/ dwGap_)* dwGap_;
DWORD dwGuardSize = dwChunkSize - dwUseSize;
nth_index<MemoryTable,1>::type &idx=get<1>(FreeTable);
nth_index_iterator<MemoryTable,1>::type it = idx.lower_bound(dwUseSize);
nth_index<MemoryTable,2>::type &idx2=get<2>(FreeTable);
nth_index_iterator<MemoryTable,2>::type it2;
//ASSERT(it->dwSize >= dwSize);
do
{
it2 = idx2.lower_bound(dwChunkSize);
if ( it != idx.end() && it->m_dwChunkSize >= dwChunkSize )
{//Find Invoke Free Reusable Memory
//MemoryTrunk allocateTrunk , freeTrunk;
// (*LogMessage)<< log4cpp::Priority::INFO
// <<__FUNCTION__<<" find free chunk "<<"dwChunkSize = "<<dwChunkSize;
MemoryTrunk freeTrunk = *it;
idx.erase(it);
//Not Need To Merge
freeCache.erase(freeTrunk.m_lpVoid);
assert(freeTrunk.m_dwUseSize >= dwUseSize && freeTrunk.m_dwChunkSize >= dwChunkSize);
if (//dwChunkSize == freeTrunk.dwChunkSize
/*&&*/ freeTrunk.m_dwUseSize == dwUseSize)
{
assert(freeTrunk.m_dwChunkSize >= dwChunkSize );
if (freeTrunk.m_dwChunkSize == dwChunkSize)
{//accuracy Match Memory Block
MemoryTrunk allocateTrunk = freeTrunk;/*(
没有合适的资源?快使用搜索试试~ 我知道了~
采用boost内存数据库技术和线程池技术开发的内存池技术,支持内存回收,碎片合并
共42个文件
h:16个
old:6个
cpp:6个
4星 · 超过85%的资源 需积分: 10 94 下载量 172 浏览量
2009-08-27
10:12:08
上传
评论
收藏 18KB 7Z 举报
温馨提示
采用boost内存数据库技术和线程池技术开发的内存池技术,支持内存回收,碎片合并
资源推荐
资源详情
资源评论
收起资源包目录
Linux MemoryPool.1.3.7z (42个子文件)
MemoryPool
.project 2KB
globalDefine
GlobalDef.h 1KB
Constant.h 208B
CVS
Entries 245B
Entries.Extra 172B
Repository 63B
Entries.Old 190B
Entries.Extra.Old 122B
Root 43B
globalAlgorithm.h 867B
BoostConfigure.h 4KB
AllocatorAdapter.h 967B
Interface.cpp 443B
.cproject 47KB
CVS
Entries 697B
Entries.Extra 507B
Repository 50B
Entries.Old 543B
Entries.Extra.Old 367B
Root 43B
MemoryTrunk.cpp 6KB
MemoryPoolAllocator.h 2KB
MemoryPool.cpp 51KB
MemoryPool.h 7KB
Allocate.h 2KB
MemoryPoolBase.h 369B
Allocate.cpp 4KB
Interface.h 502B
MemoryPoolAllocator.cpp 2KB
MemoryTrunk.h 1KB
IAllocator.h 1KB
util
CVS
Entries 257B
Entries.Extra 184B
Repository 55B
Entries.Old 202B
Entries.Extra.Old 134B
Root 43B
ThreadPoolSingleton.h 3KB
SingletonHolder.h 1KB
ObjectHandle.h 4KB
Log4cppWapper.cpp 725B
Log4cppWapper.h 903B
共 42 条
- 1
资源评论
- xiaoxigua_19842012-04-03纯代码,没有任何说明,很学习
- 「已注销」2014-02-14很好的代码,谢谢分享。
- qq_355994192018-01-15很不错的代码
- 沉默圣者2013-05-07没文档 可惜了 很好的代码 值得参看~
- angelball2013-01-10很有参考价值,不过就是没有注释和相应说明。
fgmailbox
- 粉丝: 7
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功