ObjectPool.hpp:
#pragma once
#include <memory>
#include <queue>
#include <vector>
#include <assert.h>
#ifndef nullptr
#define nullptr 0
#endif
template< typename T >
class ObjectPool
{
public:
//初始化块大小如果能预先估算出块大小则性能达到最大
//即:只分配一次内存只释放一次内存
ObjectPool( size_t ChunkSize = 32 );
virtual ~ObjectPool();
//设置块大小
void SetChunkSize( size_t ChunkSize );
//模拟new关键字分配内存并调用构造函数
T* New();
//模拟delete关键字释放内存调用析构函数
void Delete( T *pT );
private:
//分配块大小
void AllocateChunk( size_t ChunkSize );
//释放块
void ReleaseChunk();
private:
//空闲池
std::queue< T* > mFreePool;
//块池
std::vector< T* > mChunkPool;
//当前块大小
size_t mChunkSize;
};
template< typename T >
ObjectPool< T >::ObjectPool( size_t ChunkSize )
:mChunkSize( ChunkSize )
{}
template< typename T >
ObjectPool< T >::~ObjectPool()
{
ReleaseChunk();
}
template< typename T >
void