A fast memory Pool used by LogMicroscope@35
As you know, new/delete operations take a lot of CPU time. If you work with servers, CPU time is important. If additional memory is added to the server, then the servers’ available memory size will grow in a linear fashion.
So Log Microscope has it’s own efficient memory management system. CFastMemoryPool is the one of them for LogMicroscope@35. because I never need to delete the instance,so a single call to Shrink() will free all memory allocated.it’s extreme fast,enjoy it!
{
public:
typedef struct tagPlex
{
tagPlex *_next;
tagPlex *_free;
size_t _freeindex;
#ifndef _WIN64
#if (_AFX_PACKING >= 8)
DWORD dwReserved[1]; // align on 8 byte boundary
#endif
#endif
inline void *data()
{
return this + 1;
}
}PLEX, *PPLEX;
CFastMemoryPool()
{
m_pBlock = NULL;
m_pFreeBlock = 0;
m_nBlockSize = 0;
m_nNumberOfObjectsInSegment = 0;
m_nNumberOfSegmentsStrat = 0;
m_nObjectSize = 0;
m_nAllocatedSegment = 0;
}
~CFastMemoryPool()
{
Destroy();
}
operator bool()const
{
return m_pBlock != NULL;
}
void *AllocBuffer();
void Shrink();
void Destroy();
HRESULT Initialize(size_t nObjectSize, size_t nNumberOfBuffersInSegment,
size_t nNumberOfSegmentsStrat);
private:
PPLEX AllocBlock();
PPLEX m_pBlock;
PPLEX m_pFreeBlock;
size_t m_nBlockSize;
size_t m_nAllocatedSegment;
size_t m_nNumberOfObjectsInSegment;
size_t m_nNumberOfSegmentsStrat;
size_t m_nObjectSize;
};
HRESULT CFastMemoryPool::Initialize(size_t nObjectSize, size_t nNumberOfObjectsInSegment,
size_t nNumberOfSegmentsStrat)
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
m_nObjectSize = nObjectSize;
m_nBlockSize = MemAlign(nNumberOfObjectsInSegment * nObjectSize + sizeof( PLEX ),
sysinfo.dwAllocationGranularity);
m_nAllocatedSegment = 0;
m_nNumberOfSegmentsStrat = nNumberOfSegmentsStrat;
m_nNumberOfObjectsInSegment = nNumberOfObjectsInSegment;
for (size_t i = 0; i < nNumberOfSegmentsStrat; ++i)
{
AllocBlock();
}
return S_OK;
}
CFastMemoryPool::PPLEX CFastMemoryPool::AllocBlock()
{
PPLEX pBlock = static_cast< PPLEX >( VirtualAlloc(NULL,
m_nBlockSize, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE) );
++m_nAllocatedSegment;
pBlock->_next = m_pBlock;
m_pBlock = pBlock;
pBlock->_freeindex = 0;
pBlock->_free = m_pFreeBlock;
m_pFreeBlock = pBlock;
return pBlock;
}
void *CFastMemoryPool::AllocBuffer()
{
while (m_pFreeBlock)
{
if (m_pFreeBlock->_freeindex < m_nNumberOfObjectsInSegment)
{
return ((BYTE *)m_pFreeBlock->data() + m_nObjectSize
* m_pFreeBlock->_freeindex++);
}
m_pFreeBlock = m_pFreeBlock->_free;
}
AllocBlock();
return ((BYTE *)m_pFreeBlock->data() + m_nObjectSize * m_pFreeBlock->_freeindex++);
}
void CFastMemoryPool::Shrink()
{
for (size_t i = m_nNumberOfSegmentsStrat; i < m_nAllocatedSegment; ++i)
{
PPLEX pKill = m_pBlock;
m_pBlock = m_pBlock->_next;
::VirtualFree(pKill, 0, MEM_RELEASE);
– m_nAllocatedSegment;
}
m_pFreeBlock = NULL;
PPLEX pPlex = m_pBlock;
while (pPlex)
{
pPlex->_freeindex = 0;
pPlex->_free = m_pFreeBlock;
m_pFreeBlock = pPlex;
pPlex=pPlex->_next;
}
}
void CFastMemoryPool::Destroy()
{
PPLEX pPlex = m_pBlock;
while (m_pBlock)
{
PPLEX pKill = m_pBlock;
m_pBlock = m_pBlock->_next;
::VirtualFree(pKill, 0, MEM_RELEASE);
}
m_pFreeBlock = NULL;
m_nAllocatedSegment = 0;
}
I have received the following compiler errors:
fastmemorypool.h(61) : error C3861: 'MemAlign': identifier not found
fastmemorypool.h(108) : error C2065: '' : undeclared identifier
fastmemorypool.h(108) : error C2146: syntax error : missing ';' before identifier 'm_nAllocatedSegment'