Update CMemStack to 64bit

Use unsigned 64bit integer types for all size types, and use 64bit format specifier. Also use 64bit pointer types/value assignments. The virtual class and overrides no longer exist, these have been commented.
This commit is contained in:
Kawe Mazidjatari 2022-11-29 01:45:24 +01:00
parent 3a4a5a08f8
commit 25aa8f02c0
2 changed files with 89 additions and 83 deletions

View File

@ -35,12 +35,14 @@ void PrintStatus( void* p )
}
CMemoryStack::CMemoryStack()
: m_pNextAlloc( NULL )
, m_pCommitLimit( NULL )
, m_pAllocLimit( NULL )
, m_pHighestAllocLimit( NULL )
, m_pBase( NULL )
: m_pNextAlloc( nullptr )
, m_pCommitLimit( nullptr )
, m_pAllocLimit( nullptr )
, m_pHighestAllocLimit( nullptr )
, m_pBase( nullptr )
, m_pUnkPtr( nullptr )
, m_bRegisteredAllocation( false )
, m_unkSize( 0 )
, m_maxSize( 0 )
, m_alignment( 16 )
#ifdef MEMSTACK_VIRTUAL_MEMORY_AVAILABLE
@ -51,7 +53,7 @@ CMemoryStack::CMemoryStack()
#endif
#endif
{
AddMemoryInfoCallback( this );
//AddMemoryInfoCallback( this );
m_pszAllocOwner = strdup( "CMemoryStack unattributed" );
}
@ -62,13 +64,13 @@ CMemoryStack::~CMemoryStack()
if ( m_pBase )
Term();
RemoveMemoryInfoCallback( this );
//RemoveMemoryInfoCallback( this );
free( m_pszAllocOwner );
}
//-------------------------------------
bool CMemoryStack::Init( const char *pszAllocOwner, unsigned maxSize, unsigned commitIncrement, unsigned initialCommit, unsigned alignment )
bool CMemoryStack::Init( const char *pszAllocOwner, uint64 maxSize, uint64 commitIncrement, uint64 initialCommit, uint64 alignment )
{
Assert( !m_pBase );
@ -96,7 +98,7 @@ bool CMemoryStack::Init( const char *pszAllocOwner, unsigned maxSize, unsigned c
m_commitIncrement = commitIncrement;
}
unsigned pageSize;
uint64 pageSize;
#ifdef _PS3
pageSize = PS3_PAGE_SIZE;
@ -179,7 +181,7 @@ bool CMemoryStack::Init( const char *pszAllocOwner, unsigned maxSize, unsigned c
m_pAllocLimit = m_pBase + m_maxSize;
return ( m_pBase != NULL );
return ( m_pBase != nullptr );
}
//-------------------------------------
@ -257,12 +259,13 @@ void CMemoryStack::Term()
#else
MemAlloc_FreeAligned( m_pBase );
#endif
m_pBase = NULL;
m_pBase = nullptr;
// Zero these variables to avoid getting misleading mem_dump
// results when m_pBase is NULL.
m_pNextAlloc = NULL;
m_pCommitLimit = NULL;
m_pHighestAllocLimit = NULL;
m_pNextAlloc = nullptr;
m_pCommitLimit = nullptr;
m_pHighestAllocLimit = nullptr;
m_pUnkPtr = nullptr;
m_maxSize = 0;
RegisterDeallocation(true);
}
@ -270,7 +273,7 @@ void CMemoryStack::Term()
//-------------------------------------
int CMemoryStack::GetSize() const
uint64 CMemoryStack::GetSize() const
{
if ( m_bPhysical )
return m_maxSize;
@ -289,7 +292,7 @@ bool CMemoryStack::CommitTo( byte *pNextAlloc ) RESTRICT
{
if ( m_bPhysical )
{
return NULL;
return false;
}
#ifdef MEMSTACK_VIRTUAL_MEMORY_AVAILABLE
@ -433,31 +436,31 @@ void CMemoryStack::FreeAll( bool bDecommit )
//-------------------------------------
void CMemoryStack::Access( void **ppRegion, unsigned *pBytes )
void CMemoryStack::Access( void **ppRegion, uint64 *pBytes )
{
*ppRegion = m_pBase;
*pBytes = ( m_pNextAlloc - m_pBase);
}
const char* CMemoryStack::GetMemoryName() const
{
return m_pszAllocOwner;
}
size_t CMemoryStack::GetAllocatedBytes() const
{
return GetUsed();
}
size_t CMemoryStack::GetCommittedBytes() const
{
return GetSize();
}
size_t CMemoryStack::GetReservedBytes() const
{
return GetMaxSize();
}
//const char* CMemoryStack::GetMemoryName() const
//{
// return m_pszAllocOwner;
//}
//
//size_t CMemoryStack::GetAllocatedBytes() const
//{
// return GetUsed();
//}
//
//size_t CMemoryStack::GetCommittedBytes() const
//{
// return GetSize();
//}
//
//size_t CMemoryStack::GetReservedBytes() const
//{
// return GetMaxSize();
//}
//size_t CMemoryStack::GetHighestBytes() const
//{
@ -467,22 +470,22 @@ size_t CMemoryStack::GetReservedBytes() const
//-------------------------------------
//void CMemoryStack::PrintContents() const
//{
// size_t highest = m_pHighestAllocLimit - m_pBase;
// MEMORY_BASIC_INFORMATION info;
// char moduleName[260];
// strcpy( moduleName, "unknown module" );
// // Because this code is statically linked into each DLL, this function and the PrintStatus
// // function will be in the DLL that constructed the CMemoryStack object. We can then
// // retrieve the DLL name to give slightly more verbose memory dumps.
// if ( VirtualQuery( &PrintStatus, &info, sizeof( info ) ) == sizeof( info ) )
// {
// GetModuleFileNameA( (HMODULE) info.AllocationBase, moduleName, _countof( moduleName ) );
// moduleName[ _countof( moduleName )-1 ] = 0;
// }
// DevMsg( eDLL_T::COMMON, "CMemoryStack %s in %s\n", m_pszAllocOwner, moduleName );
// DevMsg( eDLL_T::COMMON, " Total used memory: %d KB\n", GetUsed() / 1024 );
// DevMsg( eDLL_T::COMMON, " Total committed memory: %d KB\n", GetSize() / 1024 );
// DevMsg( eDLL_T::COMMON, " Max committed memory: %u KB out of %d KB\n", (unsigned)highest / 1024, GetMaxSize() / 1024 );
//}
void CMemoryStack::PrintContents() const
{
size_t highest = m_pHighestAllocLimit - m_pBase;
MEMORY_BASIC_INFORMATION info;
char moduleName[260];
strcpy( moduleName, "unknown module" );
// Because this code is statically linked into each DLL, this function and the PrintStatus
// function will be in the DLL that constructed the CMemoryStack object. We can then
// retrieve the DLL name to give slightly more verbose memory dumps.
if ( VirtualQuery( &PrintStatus, &info, sizeof( info ) ) == sizeof( info ) )
{
GetModuleFileNameA( (HMODULE) info.AllocationBase, moduleName, _countof( moduleName ) );
moduleName[ _countof( moduleName )-1 ] = 0;
}
DevMsg( eDLL_T::COMMON, "CMemoryStack %s in %s\n", m_pszAllocOwner, moduleName );
DevMsg( eDLL_T::COMMON, " Total used memory: %zu KB\n", GetUsed() / 1024 );
DevMsg( eDLL_T::COMMON, " Total committed memory: %zu KB\n", GetSize() / 1024 );
DevMsg( eDLL_T::COMMON, " Max committed memory: %zu KB out of %zu KB\n", highest / 1024, GetMaxSize() / 1024 );
}

View File

@ -20,38 +20,35 @@
//-----------------------------------------------------------------------------
typedef unsigned MemoryStackMark_t;
typedef uint64 MemoryStackMark_t;
class CMemoryStack : private IMemoryInfo
class CMemoryStack //: private IMemoryInfo
{
public:
CMemoryStack();
~CMemoryStack();
bool Init( const char *pszAllocOwner, unsigned maxSize = 0, unsigned commitIncrement = 0, unsigned initialCommit = 0, unsigned alignment = 16 );
#ifdef _GAMECONSOLE
bool InitPhysical( const char *pszAllocOwner, uint size, uint nBaseAddrAlignment, uint alignment = 16, uint32 nAdditionalFlags = 0 );
#endif
bool Init( const char *pszAllocOwner, uint64 maxSize = 0, uint64 commitIncrement = 0, uint64 initialCommit = 0, uint64 alignment = 16 );
void Term();
int GetSize() const;
int GetMaxSize() const ;
int GetUsed() const;
uint64 GetSize() const;
uint64 GetMaxSize() const ;
uint64 GetUsed() const;
void *Alloc( unsigned bytes, bool bClear = false ) RESTRICT;
void *Alloc( uint64 bytes, bool bClear = false ) RESTRICT;
MemoryStackMark_t GetCurrentAllocPoint() const;
void FreeToAllocPoint( MemoryStackMark_t mark, bool bDecommit = true );
void FreeAll( bool bDecommit = true );
void Access( void **ppRegion, unsigned *pBytes );
void Access( void **ppRegion, uint64 *pBytes );
void PrintContents() const;
void *GetBase();
const void *GetBase() const { return const_cast<CMemoryStack *>(this)->GetBase(); }
bool CommitSize( int nBytes );
bool CommitSize( uint64 nBytes );
void SetAllocOwner( const char *pszAllocOwner );
@ -60,28 +57,33 @@ private:
void RegisterAllocation();
void RegisterDeallocation( bool bShouldSpew );
const char* GetMemoryName() const OVERRIDE; // User friendly name for this stack or pool
size_t GetAllocatedBytes() const OVERRIDE; // Number of bytes currently allocated
size_t GetCommittedBytes() const OVERRIDE; // Bytes committed -- may be greater than allocated.
size_t GetReservedBytes() const OVERRIDE; // Bytes reserved -- may be greater than committed.
size_t GetHighestBytes() const OVERRIDE; // The maximum number of bytes allocated or committed.
//const char* GetMemoryName() const OVERRIDE; // User friendly name for this stack or pool
//size_t GetAllocatedBytes() const OVERRIDE; // Number of bytes currently allocated
//size_t GetCommittedBytes() const OVERRIDE; // Bytes committed -- may be greater than allocated.
//size_t GetReservedBytes() const OVERRIDE; // Bytes reserved -- may be greater than committed.
//size_t GetHighestBytes() const OVERRIDE; // The maximum number of bytes allocated or committed.
byte *m_pNextAlloc; // Current alloc point (m_pNextAlloc - m_pBase == allocated bytes)
byte *m_pCommitLimit; // The current end of the committed memory. On systems without dynamic commit/decommit this is always m_pAllocLimit
byte *m_pAllocLimit; // The top of the allocated address space (m_pBase + m_maxSize)
// Track the highest alloc limit seen.
byte *m_pHighestAllocLimit;
byte *m_pBase;
// Track the highest alloc limit seen.
byte* m_pHighestAllocLimit; // This field probably no longer exist, but there is a 64bit type at this offset.
byte* m_pUnkPtr; // Unknown..
bool m_bRegisteredAllocation;
bool m_bPhysical;
char *m_pszAllocOwner;
unsigned m_maxSize; // m_maxSize stores how big the stack can grow. It measures the reservation size.
unsigned m_alignment;
uint64 m_unkSize; // Unknown field..
uint64 m_maxSize; // m_maxSize stores how big the stack can grow. It measures the reservation size.
uint64 m_alignment;
#ifdef MEMSTACK_VIRTUAL_MEMORY_AVAILABLE
unsigned m_commitIncrement;
unsigned m_minCommit;
uint64 m_commitIncrement;
uint64 m_minCommit;
#endif
#if defined( MEMSTACK_VIRTUAL_MEMORY_AVAILABLE ) && defined( _PS3 )
IVirtualMemorySection *m_pVirtualMemorySection;
@ -95,8 +97,9 @@ private:
//-------------------------------------
FORCEINLINE void *CMemoryStack::Alloc( unsigned bytes, bool bClear ) RESTRICT
FORCEINLINE void *CMemoryStack::Alloc( uint64 bytes, bool bClear ) RESTRICT
{
sizeof(CMemoryStack);
Assert( m_pBase );
bytes = MAX( bytes, m_alignment );
@ -109,7 +112,7 @@ FORCEINLINE void *CMemoryStack::Alloc( unsigned bytes, bool bClear ) RESTRICT
{
if ( !CommitTo( pNextAlloc ) )
{
return NULL;
return nullptr;
}
}
@ -126,7 +129,7 @@ FORCEINLINE void *CMemoryStack::Alloc( unsigned bytes, bool bClear ) RESTRICT
//-------------------------------------
inline bool CMemoryStack::CommitSize( int nBytes )
inline bool CMemoryStack::CommitSize( uint64 nBytes )
{
if ( GetSize() != nBytes )
{
@ -139,14 +142,14 @@ inline bool CMemoryStack::CommitSize( int nBytes )
// How big can this memory stack grow? This is equivalent to how many
// bytes are reserved.
inline int CMemoryStack::GetMaxSize() const
inline uint64 CMemoryStack::GetMaxSize() const
{
return m_maxSize;
}
//-------------------------------------
inline int CMemoryStack::GetUsed() const
inline uint64 CMemoryStack::GetUsed() const
{
return ( m_pNextAlloc - m_pBase );
}