对象池的指针版本。池中本质上是一群对象,只构造一次,返回指针。主要支持频繁重建的内存空间不连续的自建结构,如树,链表。
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//******************************************************************
*名称:PointerPool(指针池)(模板)
*版本号:0.1
*作者:赵耀(中山大学2010级)
*时间:2011.4.11
*简介:
* 对应于对象池,这是一个指针池,用于缓存频繁调用的指针(对象).主要支
* 持频繁重建的树或链表等结构.具有默认构造函数接收一个size_t对象表明指针
* 分块的大小,默认为10.接收的参数如果<=0则会抛出异常invalid_argument.
* public方法:
* T* getPointer(); 从池中返回一个该类型对象的指针;
* void recyclePointer( T* ); 接受需要回收的指针;
* void clear(); 清空池以释放占用的内存;
* bool empty(); 返回池是否为空;
* size_t size(); 返回池中总指针数量;
* size_t remain(); 返回指针池剩余可用指针的数量;
* 注意:
* 使用方法可简单地想成通过两个成员函数来代替new和delete.必须确保分
* 配的指针得到回收!必须确保回收的指针不是外来指针!因为未进行二次初始化,
* 重新分配得到的指针所指对象可能是上一次使用后的残余对象,请进行必要的初
* 话工作.
*
*未完成特性:
* 因为主要支持树,链表等内存空间不连续的结构,所以未实现成数组规模
* 地分配指针的功能.
*
*已知bug:暂无
*
*版权信息:
* 该代码为开源代码,原作者保留其所有权.你可以拷贝,修改,使用该代码,但
* 是请保留必要的版权信息.
******************************************************************/
#ifndef POINTERPOOL_H
#define POINTERPOOL_H
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <vector>
#include <queue>
#include <stdexcept>
#include <memory>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
class PointerPool
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
//Create a PointerPool to contain size pointers.
PointerPool( size_t size = kDefaultChunkSize )
throw ( invalid_argument, bad_alloc );
~PointerPool();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Return a specific pointer to client.
T *getPointer();
//Recycle the pointer that the client doesn't need any more.
void recyclePointer( T* );
//Clear the pool and release the memory.
void clear();
bool empty();
//Return the total number of pointers the pool contains.
size_t size();
//Return the number of pointers remain available.
size_t remain();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
protected:
queue< T* > mFreeList;
vector< T* > mAllpointers;//A record of all pointers which help
//to destroy them.
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
size_t mChunkSize;
static const size_t kDefaultChunkSize = 10;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Allocate mChunkSize new pointers and add them to the
//mFreeList.
void allocateChunk();
//help the destructor the delete the pointers int the pool.
static void deleteHelper( T* );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
private:
//Hide the copy constructor and assignment symbol.
PointerPool( const PointerPool< T > & );
PointerPool< T > &operator=( const PointerPool< T > & );
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
size_t PointerPool<T>::remain()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return mFreeList.size();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
bool PointerPool<T>::empty()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return remain() == 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
size_t PointerPool<T>::size()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return mChunkSize * mAllpointers.size();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
PointerPool<T>::PointerPool( size_t size /**//*= kDefaultChunkSize */ ) throw ( invalid_argument, bad_alloc )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ( size <= 0 )
throw invalid_argument( "chunk size must be positive" );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mChunkSize = size;
allocateChunk();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
T *PointerPool<T>::getPointer()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ( mFreeList.empty() )
allocateChunk();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
T *ptr = mFreeList.front();
mFreeList.pop();
return ptr;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
void PointerPool<T>::clear()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for_each( mAllpointers.begin(), mAllpointers.end(), deleteHelper );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mAllpointers.clear();
while ( !mFreeList.empty() )
mFreeList.pop();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
void PointerPool<T>::recyclePointer( T *ptr )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
mFreeList.push( ptr );
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
void PointerPool<T>::allocateChunk()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
T* newPointerChunk = new T[ mChunkSize ];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mAllpointers.push_back( newPointerChunk );
for ( int i = 0; i < mChunkSize; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mFreeList.push( &newPointerChunk[i] );
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
void PointerPool<T>::deleteHelper( T *pointerChunk )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
delete [] pointerChunk;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
template< typename T >
PointerPool<T>::~PointerPool()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for_each( mAllpointers.begin(), mAllpointers.end(), deleteHelper );
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#endif
以下为测试代码:
#include "PointerPool.h"
#include <iostream>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class ListNode
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
int value;
ListNode *nextPtr;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ListNode() : value(0), nextPtr(0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int growing()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
static int numGrouwing = 0;
return numGrouwing++;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
const int cases = 2;
PointerPool< ListNode > lnPool;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Make 2 tests.
for ( int i = 0; i < cases; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( lnPool.empty() )
cout << "The pool is empty!" << endl;
else
cout << "The pool has " << lnPool.remain()
<< " pointers available" << endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Create the head of a list.
ListNode *start = lnPool.getPointer();
start->value = growing();
ListNode *last = start;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Complete the list with length of 100.
for ( int j = 1; j < 100; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ListNode *tmp = lnPool.getPointer();
tmp->value = growing();
last->nextPtr = tmp;
last = tmp;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Travel and cout the value of each node.
ListNode *current = start;
while ( current != 0 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << current->value << '\t';
current = current->nextPtr;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//Recycle all pointers back to the pool.
while ( start != 0 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
current = start;
start = start->nextPtr;
lnPool.recyclePointer( current );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << "The pool has " << lnPool.size() << " pointers"
<< "\n\n";
//Try clear the pool
lnPool.clear();
}
}