I'm trying to make a memory pool, I've got everything sorted out except for one thing: the allocation. I have no idea how to return a segment of the data in the pool. I have it like this byte *m_pData;
. How would I be able to return a segment of that when it's initialized like m_pData = new byte[ size ];
Here is my code:
#define POOL_DEFAULT_SIZE 262144 //256k
CMemPool::CMemPool( void )
{
Init( POOL_DEFAULT_SIZE );
}
CMemPool::CMemPool( int size )
{
Init( size );
}
CMemPool::~CMemPool( void )
{
Destroy( );
}
void CMemPool::Init( int size )
{
m_pData = new byte[ size ];
m_nMaxSize = size;
m_nCurSize = 0;
}
void *CMemPool::Alloc( int size )
{
// this is what i can't figure out
}
void CMemPool::Free( void *mem )
{
if( !mem )
return;
memset( mem, 0, sizeof( mem ) );
mem = NULL;
}
void CMemPool::Clear( void )
{
memset( m_pData, 0, m_nMaxSize );
}
void CMemPool::Destroy( void )
{
delete m_pData;
}