📄 platformmempool.h
字号:
//内存池管理,针对频繁创建类对象最适用不过了
// 李亦.2006.5.28
//=============================================================================
#ifndef _PLATFORMMEMPOOL_H_
#define _PLATFORMMEMPOOL_H_
#ifdef _WIN32
#pragma once
#endif
//#ifdef new
//#undef new
//#endif
//-----------------------------------------------------------------------------
// Purpose: Optimized pool memory allocator
//-----------------------------------------------------------------------------
typedef void (*MemoryPoolReportFunc_t)( char const* pMsg, ... );
class PlatformMemPool
{
public:
// Ways the memory pool can grow when it needs to make a new blob.
enum
{
GROW_NONE=0, // Don't allow new blobs.
GROW_FAST=1, // New blob size is numElements * (i+1) (ie: the blocks it allocates
// get larger and larger each time it allocates one).
GROW_SLOW=2 // New blob size is numElements.
};
PlatformMemPool(int blockSize, int numElements, int growMode = GROW_FAST);
~PlatformMemPool();
void* Alloc(); // Allocate the element size you specified in the constructor.
void* Alloc( unsigned int amount );
void Free(void *pMem);
// Frees everything
void Clear();
// Error reporting...
static void SetErrorReportFunc( MemoryPoolReportFunc_t func );
private:
class CBlob
{
public:
CBlob *m_pPrev, *m_pNext;
int m_NumBytes; // Number of bytes in this blob.
char m_Data[1];
};
// Resets the pool
void Init();
void AddNewBlob();
void ReportLeaks();
private:
int m_BlockSize;
int m_BlocksPerBlob;
int m_GrowMode; // GROW_ enum.
// FIXME: Change m_ppMemBlob into a growable array?
CBlob m_BlobHead;
void *m_pHeadOfFreeList;
int m_BlocksAllocated;
int m_PeakAlloc;
unsigned short m_NumBlobs;
static MemoryPoolReportFunc_t g_ReportFunc;
};
//-----------------------------------------------------------------------------
// Wrapper macro to make an allocator that returns particular typed allocations
// and construction and destruction of objects.
//-----------------------------------------------------------------------------
template< class T >
class CClassMemoryPool : public PlatformMemPool
{
public:
CClassMemoryPool(int numElements, int growMode = GROW_FAST) :
PlatformMemPool( sizeof(T), numElements, growMode ) {}
T* Alloc();
void Free( T *pMem );
};
template< class T >
T* CClassMemoryPool<T>::Alloc()
{
T *pRet = (T*)PlatformMemPool::Alloc();
if ( pRet )
{
Construct( pRet );
}
return pRet;
}
template< class T >
void CClassMemoryPool<T>::Free(T *pMem)
{
if ( pMem )
{
Destruct( pMem );
}
PlatformMemPool::Free( pMem );
}
//-----------------------------------------------------------------------------
// 以下宏定义,能帮我们分配固定的内存块
// Macros that make it simple to make a class use a fixed-size allocator
// Put DECLARE_FIXEDSIZE_ALLOCATOR in the private section of a class,
// Put DEFINE_FIXEDSIZE_ALLOCATOR in the CPP file
//-----------------------------------------------------------------------------
#define DECLARE_FIXEDSIZE_ALLOCATOR( _class ) \
public: \
inline void* operator new( dsize_t size ) { return s_Allocator.Alloc(size); } \
inline void operator delete( void* p ) { s_Allocator.Free(p); } \
inline void operator delete( void* p, const char *pFileName, const U32 nLine ) { s_Allocator.Free(p); } \
inline void* operator new( dsize_t size, const char *pFileName, const U32 nLine ) { return s_Allocator.Alloc(size); } \
private: \
static PlatformMemPool s_Allocator
#define DEFINE_FIXEDSIZE_ALLOCATOR( _class, _initsize, _grow ) \
PlatformMemPool _class::s_Allocator(sizeof(_class), _initsize, _grow)
//-----------------------------------------------------------------------------
// 以下宏定义,能帮我们分配固定的内存块,主要是能引用外部定义的内存池
// Macros that make it simple to make a class use a fixed-size allocator
// This version allows us to use a memory pool which is externally defined...
// Put DECLARE_FIXEDSIZE_ALLOCATOR_EXTERNAL in the private section of a class,
// Put DEFINE_FIXEDSIZE_ALLOCATOR_EXTERNAL in the CPP file
//-----------------------------------------------------------------------------
#define DECLARE_FIXEDSIZE_ALLOCATOR_EXTERNAL( _class, _allocator ) \
public: \
inline void* operator new( dsize_t size ) { return s_pAllocator->Alloc(size); } \
inline void* operator new( dsize_t size, const char *pFileName, const U32 nLine ) { return s_pAllocator->Alloc(size); } \
inline void operator delete( void* p ) { s_pAllocator->Free(p); } \
private: \
static PlatformMemPool* s_pAllocator
#define DEFINE_FIXEDSIZE_ALLOCATOR_EXTERNAL( _class, _allocator ) \
PlatformMemPool* _class::s_pAllocator = _allocator
#endif // _PLATFORMMEMPOOL_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -