vphysics: fix saverestore

This commit is contained in:
nillerusr
2022-06-14 13:09:10 +03:00
parent b84ad64171
commit 94916a20ce
10 changed files with 300 additions and 122 deletions

View File

@@ -1,4 +1,4 @@
//========= Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
@@ -9,8 +9,14 @@
#include "basetypes.h"
#include "datamanager.h"
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
DECLARE_POINTER_HANDLE( memhandle_t );
#define AUTO_LOCK_DM() AUTO_LOCK_( CDataManagerBase, *this )
CDataManagerBase::CDataManagerBase( unsigned int maxSize )
{
m_targetMemorySize = maxSize;
@@ -19,11 +25,12 @@ CDataManagerBase::CDataManagerBase( unsigned int maxSize )
m_lockList = m_memoryLists.CreateList();
m_freeList = m_memoryLists.CreateList();
m_listsAreFreed = 0;
m_freeOnDestruct = 1;
}
CDataManagerBase::~CDataManagerBase()
{
Assert( m_listsAreFreed );
Assert( !m_freeOnDestruct || m_listsAreFreed );
}
void CDataManagerBase::NotifySizeChanged( memhandle_t handle, unsigned int oldSize, unsigned int newSize )
@@ -43,7 +50,7 @@ unsigned int CDataManagerBase::FlushAllUnlocked()
Lock();
int nFlush = m_memoryLists.Count( m_lruList );
void **pScratch = (void **)_alloca( nFlush * sizeof(void *) );
void **pScratch = (void **)stackalloc( nFlush * sizeof(void *) );
CUtlVector<void *> destroyList( pScratch, nFlush );
unsigned nBytesInitial = MemUsed_Inline();
@@ -80,7 +87,7 @@ unsigned int CDataManagerBase::FlushAll()
Lock();
int nFlush = m_memoryLists.Count( m_lruList ) + m_memoryLists.Count( m_lockList );
void **pScratch = (void **)_alloca( nFlush * sizeof(void *) );
void **pScratch = (void **) stackalloc( nFlush * sizeof(void *) );
CUtlVector<void *> destroyList( pScratch, nFlush );
unsigned result = MemUsed_Inline();
@@ -120,8 +127,7 @@ unsigned int CDataManagerBase::FlushAll()
unsigned int CDataManagerBase::Purge( unsigned int nBytesToPurge )
{
unsigned int nTargetSize = MemUsed_Inline() - nBytesToPurge;
// Check for underflow
if ( MemUsed_Inline() < nBytesToPurge )
if ( nBytesToPurge > MemUsed_Inline() )
nTargetSize = 0;
unsigned int nImpliedCapacity = MemTotal_Inline() - nTargetSize;
return EnsureCapacity( nImpliedCapacity );
@@ -151,8 +157,7 @@ void CDataManagerBase::DestroyResource( memhandle_t handle )
void *CDataManagerBase::LockResource( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
@@ -169,9 +174,29 @@ void *CDataManagerBase::LockResource( memhandle_t handle )
return NULL;
}
void *CDataManagerBase::LockResourceReturnCount( int *pCount, memhandle_t handle )
{
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
if ( m_memoryLists[memoryIndex].lockCount == 0 )
{
m_memoryLists.Unlink( m_lruList, memoryIndex );
m_memoryLists.LinkToTail( m_lockList, memoryIndex );
}
Assert(m_memoryLists[memoryIndex].lockCount != (unsigned short)-1);
*pCount = ++m_memoryLists[memoryIndex].lockCount;
return m_memoryLists[memoryIndex].pStore;
}
*pCount = 0;
return NULL;
}
int CDataManagerBase::UnlockResource( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
@@ -193,7 +218,7 @@ int CDataManagerBase::UnlockResource( memhandle_t handle )
void *CDataManagerBase::GetResource_NoLockNoLRUTouch( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
@@ -205,7 +230,7 @@ void *CDataManagerBase::GetResource_NoLockNoLRUTouch( memhandle_t handle )
void *CDataManagerBase::GetResource_NoLock( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
@@ -217,13 +242,13 @@ void *CDataManagerBase::GetResource_NoLock( memhandle_t handle )
void CDataManagerBase::TouchResource( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
TouchByIndex( FromHandle(handle) );
}
void CDataManagerBase::MarkAsStale( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() )
{
@@ -237,7 +262,7 @@ void CDataManagerBase::MarkAsStale( memhandle_t handle )
int CDataManagerBase::BreakLock( memhandle_t handle )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
unsigned short memoryIndex = FromHandle(handle);
if ( memoryIndex != m_memoryLists.InvalidIndex() && m_memoryLists[memoryIndex].lockCount )
{
@@ -253,7 +278,7 @@ int CDataManagerBase::BreakLock( memhandle_t handle )
int CDataManagerBase::BreakAllLocks()
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
int nBroken = 0;
int node;
int nextNode;
@@ -275,7 +300,7 @@ int CDataManagerBase::BreakAllLocks()
unsigned short CDataManagerBase::CreateHandle( bool bCreateLocked )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
int memoryIndex = m_memoryLists.Head(m_freeList);
unsigned short list = ( bCreateLocked ) ? m_lockList : m_lruList;
if ( memoryIndex != m_memoryLists.InvalidIndex() )
@@ -298,7 +323,7 @@ unsigned short CDataManagerBase::CreateHandle( bool bCreateLocked )
memhandle_t CDataManagerBase::StoreResourceInHandle( unsigned short memoryIndex, void *pStore, unsigned int realSize )
{
AUTO_LOCK( *this );
AUTO_LOCK_DM();
resource_lru_element_t &mem = m_memoryLists[memoryIndex];
mem.pStore = pStore;
m_memUsed += realSize;
@@ -322,7 +347,7 @@ memhandle_t CDataManagerBase::ToHandle( unsigned short index )
unsigned int hiword = m_memoryLists.Element(index).serial;
hiword <<= 16;
index++;
return (memhandle_t)( hiword|index );
return reinterpret_cast< memhandle_t >( (uintp)( hiword|index ) );
}
unsigned int CDataManagerBase::TargetSize()

View File

@@ -1,24 +1,23 @@
//========= Copyright Valve Corporation, All rights reserved. ============//
//===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
//===========================================================================//
#include "mempool.h"
#include "tier1/mempool.h"
#include <stdio.h>
#ifdef OSX
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <memory.h>
#include "tier0/dbg.h"
#include <ctype.h>
#include "tier1/strtools.h"
#ifndef _PS3
#include <malloc.h>
#endif
// Should be last include
#include "tier0/memdbgon.h"
MemoryPoolReportFunc_t CUtlMemoryPool::g_ReportFunc = 0;
//-----------------------------------------------------------------------------
@@ -36,7 +35,7 @@ void CUtlMemoryPool::SetErrorReportFunc( MemoryPoolReportFunc_t func )
CUtlMemoryPool::CUtlMemoryPool( int blockSize, int numElements, int growMode, const char *pszAllocOwner, int nAlignment )
{
#ifdef _X360
if( numElements > 0 && growMode != UTLMEMORYPOOL_GROW_NONE )
if( numElements > 0 && growMode != GROW_NONE )
{
numElements = 1;
}
@@ -100,18 +99,40 @@ void CUtlMemoryPool::Clear()
Init();
}
//-----------------------------------------------------------------------------
// Is an allocation within the pool?
//-----------------------------------------------------------------------------
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;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Reports memory leaks
//-----------------------------------------------------------------------------
void CUtlMemoryPool::ReportLeaks()
{
#ifdef _DEBUG
if (!g_ReportFunc)
return;
g_ReportFunc("Memory leak: mempool blocks left in memory: %d\n", m_BlocksAllocated);
#ifdef _DEBUG
// walk and destroy the free list so it doesn't intefere in the scan
while (m_pHeadOfFreeList != NULL)
{
@@ -132,7 +153,7 @@ void CUtlMemoryPool::ReportLeaks()
while (scanPoint < scanEnd)
{
// search for and dump any strings
if ((unsigned)(*scanPoint + 1) <= 256 && isprint(*scanPoint))
if ((unsigned)(*scanPoint + 1) <= 256 && V_isprint(*scanPoint))
{
g_ReportFunc("%c", *scanPoint);
needSpace = true;
@@ -161,18 +182,18 @@ void CUtlMemoryPool::AddNewBlob()
int sizeMultiplier;
if( m_GrowMode == UTLMEMORYPOOL_GROW_SLOW )
if( m_GrowMode == GROW_SLOW )
{
sizeMultiplier = 1;
}
else
{
if ( m_GrowMode == UTLMEMORYPOOL_GROW_NONE )
if ( m_GrowMode == GROW_NONE )
{
// Can only have one allocation when we're in this mode
if( m_NumBlobs != 0 )
{
Assert( !"CUtlMemoryPool::AddNewBlob: mode == UTLMEMORYPOOL_GROW_NONE" );
Assert( !"CUtlMemoryPool::AddNewBlob: mode == GROW_NONE" );
return;
}
}
@@ -230,15 +251,15 @@ void *CUtlMemoryPool::Alloc( size_t amount )
{
void *returnBlock;
if ( amount > (unsigned int)m_BlockSize )
if ( amount > (size_t)m_BlockSize )
return NULL;
if( !m_pHeadOfFreeList )
if ( !m_pHeadOfFreeList )
{
// returning NULL is fine in UTLMEMORYPOOL_GROW_NONE
if( m_GrowMode == UTLMEMORYPOOL_GROW_NONE )
// returning NULL is fine in GROW_NONE
if ( m_GrowMode == GROW_NONE && m_NumBlobs > 0 )
{
//Assert( !"CUtlMemoryPool::Alloc: tried to make new blob with UTLMEMORYPOOL_GROW_NONE" );
//Assert( !"CUtlMemoryPool::Alloc: tried to make new blob with GROW_NONE" );
return NULL;
}
@@ -246,14 +267,14 @@ void *CUtlMemoryPool::Alloc( size_t amount )
AddNewBlob();
// still failure, error out
if( !m_pHeadOfFreeList )
if ( !m_pHeadOfFreeList )
{
Assert( !"CUtlMemoryPool::Alloc: ran out of memory" );
return NULL;
}
}
m_BlocksAllocated++;
m_PeakAlloc = max(m_PeakAlloc, m_BlocksAllocated);
m_PeakAlloc = MAX(m_PeakAlloc, m_BlocksAllocated);
returnBlock = m_pHeadOfFreeList;
@@ -272,7 +293,7 @@ void *CUtlMemoryPool::AllocZero( size_t amount )
void *mem = Alloc( amount );
if ( mem )
{
V_memset( mem, 0x00, amount );
V_memset( mem, 0x00, ( int )amount );
}
return mem;
}
@@ -313,4 +334,14 @@ void CUtlMemoryPool::Free( void *memBlock )
m_pHeadOfFreeList = memBlock;
}
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;
}