2022-06-14 10:09:10 +00:00
|
|
|
//===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
|
2020-04-22 16:56:21 +00:00
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
//===========================================================================//
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
#include "tier1/mempool.h"
|
2020-04-22 16:56:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include "tier0/dbg.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "tier1/strtools.h"
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
#ifndef _PS3
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-22 16:56:21 +00:00
|
|
|
// Should be last include
|
|
|
|
#include "tier0/memdbgon.h"
|
2022-06-14 10:09:10 +00:00
|
|
|
|
2020-04-22 16:56:21 +00:00
|
|
|
MemoryPoolReportFunc_t CUtlMemoryPool::g_ReportFunc = 0;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Error reporting... (debug only)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CUtlMemoryPool::SetErrorReportFunc( MemoryPoolReportFunc_t func )
|
|
|
|
{
|
|
|
|
g_ReportFunc = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Constructor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
CUtlMemoryPool::CUtlMemoryPool( int blockSize, int numElements, int growMode, const char *pszAllocOwner, int nAlignment )
|
|
|
|
{
|
|
|
|
#ifdef _X360
|
2022-06-14 10:09:10 +00:00
|
|
|
if( numElements > 0 && growMode != GROW_NONE )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
|
|
|
numElements = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-06-21 19:15:14 +00:00
|
|
|
#ifdef PLATFORM_64BITS
|
|
|
|
m_nAlignment = ( nAlignment != 0 ) ? nAlignment : 8;
|
|
|
|
#else
|
|
|
|
m_nAlignment = ( nAlignment != 0 ) ? nAlignment : 4;
|
|
|
|
#endif
|
|
|
|
|
2020-04-22 16:56:21 +00:00
|
|
|
Assert( IsPowerOfTwo( m_nAlignment ) );
|
|
|
|
m_BlockSize = blockSize < sizeof(void*) ? sizeof(void*) : blockSize;
|
|
|
|
m_BlockSize = AlignValue( m_BlockSize, m_nAlignment );
|
|
|
|
m_BlocksPerBlob = numElements;
|
|
|
|
m_PeakAlloc = 0;
|
|
|
|
m_GrowMode = growMode;
|
|
|
|
if ( !pszAllocOwner )
|
|
|
|
{
|
|
|
|
pszAllocOwner = __FILE__;
|
|
|
|
}
|
|
|
|
m_pszAllocOwner = pszAllocOwner;
|
|
|
|
Init();
|
|
|
|
AddNewBlob();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Frees the memory contained in the mempool, and invalidates it for
|
|
|
|
// any further use.
|
|
|
|
// Input : *memPool - the mempool to shutdown
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
CUtlMemoryPool::~CUtlMemoryPool()
|
|
|
|
{
|
|
|
|
if (m_BlocksAllocated > 0)
|
|
|
|
{
|
|
|
|
ReportLeaks();
|
|
|
|
}
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Resets the pool
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CUtlMemoryPool::Init()
|
|
|
|
{
|
|
|
|
m_NumBlobs = 0;
|
|
|
|
m_BlocksAllocated = 0;
|
|
|
|
m_pHeadOfFreeList = 0;
|
|
|
|
m_BlobHead.m_pNext = m_BlobHead.m_pPrev = &m_BlobHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Frees everything
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CUtlMemoryPool::Clear()
|
|
|
|
{
|
|
|
|
// Free everything..
|
|
|
|
CBlob *pNext;
|
|
|
|
for( CBlob *pCur = m_BlobHead.m_pNext; pCur != &m_BlobHead; pCur = pNext )
|
|
|
|
{
|
|
|
|
pNext = pCur->m_pNext;
|
|
|
|
free( pCur );
|
|
|
|
}
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
|
2020-04-22 16:56:21 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-14 10:09:10 +00:00
|
|
|
// Is an allocation within the pool?
|
2020-04-22 16:56:21 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-14 10:09:10 +00:00
|
|
|
bool CUtlMemoryPool::IsAllocationWithinPool( void *pMem ) const
|
|
|
|
{
|
|
|
|
for( CBlob *pCur = m_BlobHead.m_pNext; pCur != &m_BlobHead; pCur = pCur->m_pNext )
|
|
|
|
{
|
|
|
|
// Is the allocation within the blob?
|
|
|
|
if ( ( pMem < pCur->m_Data ) || ( pMem >= pCur->m_Data + pCur->m_NumBytes ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Make sure the allocation is on a block boundary
|
|
|
|
intp pFirstAllocation = AlignValue( ( intp ) pCur->m_Data, m_nAlignment );
|
|
|
|
|
|
|
|
intp nOffset = (intp)pMem - pFirstAllocation;
|
|
|
|
return ( nOffset % m_BlockSize ) == 0;
|
|
|
|
}
|
2020-04-22 16:56:21 +00:00
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Reports memory leaks
|
|
|
|
//-----------------------------------------------------------------------------
|
2020-04-22 16:56:21 +00:00
|
|
|
void CUtlMemoryPool::ReportLeaks()
|
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
#ifdef _DEBUG
|
2020-04-22 16:56:21 +00:00
|
|
|
if (!g_ReportFunc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_ReportFunc("Memory leak: mempool blocks left in memory: %d\n", m_BlocksAllocated);
|
|
|
|
|
|
|
|
// walk and destroy the free list so it doesn't intefere in the scan
|
|
|
|
while (m_pHeadOfFreeList != NULL)
|
|
|
|
{
|
|
|
|
void *next = *((void**)m_pHeadOfFreeList);
|
|
|
|
memset(m_pHeadOfFreeList, 0, m_BlockSize);
|
|
|
|
m_pHeadOfFreeList = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_ReportFunc("Dumping memory: \'");
|
|
|
|
|
|
|
|
for( CBlob *pCur=m_BlobHead.m_pNext; pCur != &m_BlobHead; pCur=pCur->m_pNext )
|
|
|
|
{
|
|
|
|
// scan the memory block and dump the leaks
|
|
|
|
char *scanPoint = (char *)pCur->m_Data;
|
|
|
|
char *scanEnd = pCur->m_Data + pCur->m_NumBytes;
|
|
|
|
bool needSpace = false;
|
|
|
|
|
|
|
|
while (scanPoint < scanEnd)
|
|
|
|
{
|
|
|
|
// search for and dump any strings
|
2022-06-14 10:09:10 +00:00
|
|
|
if ((unsigned)(*scanPoint + 1) <= 256 && V_isprint(*scanPoint))
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
|
|
|
g_ReportFunc("%c", *scanPoint);
|
|
|
|
needSpace = true;
|
|
|
|
}
|
|
|
|
else if (needSpace)
|
|
|
|
{
|
|
|
|
needSpace = false;
|
|
|
|
g_ReportFunc(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
scanPoint++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_ReportFunc("\'\n");
|
|
|
|
#endif // _DEBUG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CUtlMemoryPool::AddNewBlob()
|
|
|
|
{
|
|
|
|
MEM_ALLOC_CREDIT_(m_pszAllocOwner);
|
|
|
|
|
|
|
|
int sizeMultiplier;
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
if( m_GrowMode == GROW_SLOW )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
|
|
|
sizeMultiplier = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
if ( m_GrowMode == GROW_NONE )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
|
|
|
// Can only have one allocation when we're in this mode
|
|
|
|
if( m_NumBlobs != 0 )
|
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
Assert( !"CUtlMemoryPool::AddNewBlob: mode == GROW_NONE" );
|
2020-04-22 16:56:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GROW_FAST and GROW_NONE use this.
|
|
|
|
sizeMultiplier = m_NumBlobs + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybe use something other than malloc?
|
|
|
|
int nElements = m_BlocksPerBlob * sizeMultiplier;
|
|
|
|
int blobSize = m_BlockSize * nElements;
|
|
|
|
CBlob *pBlob = (CBlob*)malloc( sizeof(CBlob) - 1 + blobSize + ( m_nAlignment - 1 ) );
|
|
|
|
Assert( pBlob );
|
|
|
|
|
|
|
|
// Link it in at the end of the blob list.
|
|
|
|
pBlob->m_NumBytes = blobSize;
|
|
|
|
pBlob->m_pNext = &m_BlobHead;
|
|
|
|
pBlob->m_pPrev = pBlob->m_pNext->m_pPrev;
|
|
|
|
pBlob->m_pNext->m_pPrev = pBlob->m_pPrev->m_pNext = pBlob;
|
|
|
|
|
|
|
|
// setup the free list
|
|
|
|
m_pHeadOfFreeList = AlignValue( pBlob->m_Data, m_nAlignment );
|
|
|
|
Assert (m_pHeadOfFreeList);
|
|
|
|
|
|
|
|
void **newBlob = (void**)m_pHeadOfFreeList;
|
|
|
|
for (int j = 0; j < nElements-1; j++)
|
|
|
|
{
|
|
|
|
newBlob[0] = (char*)newBlob + m_BlockSize;
|
|
|
|
newBlob = (void**)newBlob[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// null terminate list
|
|
|
|
newBlob[0] = NULL;
|
|
|
|
m_NumBlobs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* CUtlMemoryPool::Alloc()
|
|
|
|
{
|
|
|
|
return Alloc( m_BlockSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* CUtlMemoryPool::AllocZero()
|
|
|
|
{
|
|
|
|
return AllocZero( m_BlockSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Allocs a single block of memory from the pool.
|
|
|
|
// Input : amount -
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void *CUtlMemoryPool::Alloc( size_t amount )
|
|
|
|
{
|
|
|
|
void *returnBlock;
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
if ( amount > (size_t)m_BlockSize )
|
2020-04-22 16:56:21 +00:00
|
|
|
return NULL;
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
if ( !m_pHeadOfFreeList )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
// returning NULL is fine in GROW_NONE
|
|
|
|
if ( m_GrowMode == GROW_NONE && m_NumBlobs > 0 )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
//Assert( !"CUtlMemoryPool::Alloc: tried to make new blob with GROW_NONE" );
|
2020-04-22 16:56:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// overflow
|
|
|
|
AddNewBlob();
|
|
|
|
|
|
|
|
// still failure, error out
|
2022-06-14 10:09:10 +00:00
|
|
|
if ( !m_pHeadOfFreeList )
|
2020-04-22 16:56:21 +00:00
|
|
|
{
|
|
|
|
Assert( !"CUtlMemoryPool::Alloc: ran out of memory" );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_BlocksAllocated++;
|
2022-06-14 10:09:10 +00:00
|
|
|
m_PeakAlloc = MAX(m_PeakAlloc, m_BlocksAllocated);
|
2020-04-22 16:56:21 +00:00
|
|
|
|
|
|
|
returnBlock = m_pHeadOfFreeList;
|
|
|
|
|
|
|
|
// move the pointer the next block
|
|
|
|
m_pHeadOfFreeList = *((void**)m_pHeadOfFreeList);
|
|
|
|
|
|
|
|
return returnBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Allocs a single block of memory from the pool, zeroes the memory before returning
|
|
|
|
// Input : amount -
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void *CUtlMemoryPool::AllocZero( size_t amount )
|
|
|
|
{
|
|
|
|
void *mem = Alloc( amount );
|
|
|
|
if ( mem )
|
|
|
|
{
|
2022-06-14 10:09:10 +00:00
|
|
|
V_memset( mem, 0x00, ( int )amount );
|
2020-04-22 16:56:21 +00:00
|
|
|
}
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Frees a block of memory
|
|
|
|
// Input : *memBlock - the memory to free
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CUtlMemoryPool::Free( void *memBlock )
|
|
|
|
{
|
|
|
|
if ( !memBlock )
|
|
|
|
return; // trying to delete NULL pointer, ignore
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
// check to see if the memory is from the allocated range
|
|
|
|
bool bOK = false;
|
|
|
|
for( CBlob *pCur=m_BlobHead.m_pNext; pCur != &m_BlobHead; pCur=pCur->m_pNext )
|
|
|
|
{
|
|
|
|
if (memBlock >= pCur->m_Data && (char*)memBlock < (pCur->m_Data + pCur->m_NumBytes))
|
|
|
|
{
|
|
|
|
bOK = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Assert (bOK);
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
// invalidate the memory
|
|
|
|
memset( memBlock, 0xDD, m_BlockSize );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_BlocksAllocated--;
|
|
|
|
|
|
|
|
// make the block point to the first item in the list
|
|
|
|
*((void**)memBlock) = m_pHeadOfFreeList;
|
|
|
|
|
|
|
|
// the list head is now the new block
|
|
|
|
m_pHeadOfFreeList = memBlock;
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:09:10 +00:00
|
|
|
int CUtlMemoryPool::Size() const
|
|
|
|
{
|
|
|
|
uint32 size = 0;
|
|
|
|
|
|
|
|
for( CBlob *pCur=m_BlobHead.m_pNext; pCur != &m_BlobHead; pCur=pCur->m_pNext )
|
|
|
|
{
|
|
|
|
size += pCur->m_NumBytes;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2020-04-22 16:56:21 +00:00
|
|
|
|