void CMemoryStack::FreeToAllocPoint( MemoryStackMark_t mark ) { void *pAllocPoint = m_pBase + mark; Assert( pAllocPoint >= m_pBase && pAllocPoint <= m_pNextAlloc ); if ( pAllocPoint >= m_pBase && pAllocPoint < m_pNextAlloc ) { #if defined(_WIN32) unsigned char *pDecommitPoint = MemAlign( (unsigned char *)pAllocPoint, m_commitSize ); if ( pDecommitPoint < m_pBase + m_minCommit ) { pDecommitPoint = m_pBase + m_minCommit; } unsigned decommitSize = m_pCommitLimit - pDecommitPoint; if ( decommitSize > 0 ) { MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() ); VirtualFree( pDecommitPoint, decommitSize, MEM_DECOMMIT ); m_pCommitLimit = pDecommitPoint; if ( mark > 0 ) { MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); } } #endif m_pNextAlloc = (unsigned char *)pAllocPoint; } }
bool CMemoryStack::InitPhysical( unsigned size, unsigned alignment ) { m_bPhysical = true; m_maxSize = m_commitSize = size; m_alignment = AlignValue( alignment, 4 ); int flags = PAGE_READWRITE; if ( size >= 16*1024*1024 ) { flags |= MEM_16MB_PAGES; } else { flags |= MEM_LARGE_PAGES; } m_pBase = (unsigned char *)XPhysicalAlloc( m_maxSize, MAXULONG_PTR, 4096, flags ); Assert( m_pBase ); m_pNextAlloc = m_pBase; m_pCommitLimit = m_pBase + m_maxSize; m_pAllocLimit = m_pBase + m_maxSize; MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); return ( m_pBase != NULL ); }
void XMemAlloc_RegisterAllocation( void *p, DWORD dwAllocAttributes ) { if ( !g_pMemAlloc ) { // core xallocs cannot be journaled until system is ready return; } AUTO_LOCK_FM( g_XMemAllocMutex ); int size = XMemSize( p, dwAllocAttributes ); MemAlloc_RegisterExternalAllocation( XMem, p, size ); }
void *CMemoryStack::Alloc( unsigned bytes, const char *pszName ) { Assert( m_pBase ); if ( !bytes ) bytes = 1; bytes = MemAlign( bytes, m_alignment ); void *pResult = m_pNextAlloc; m_pNextAlloc += bytes; if ( m_pNextAlloc > m_pCommitLimit ) { #if defined(_WIN32) unsigned char * pNewCommitLimit = MemAlign( m_pNextAlloc, m_commitSize ); unsigned commitSize = pNewCommitLimit - m_pCommitLimit; MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() ); Assert( m_pCommitLimit + commitSize < m_pAllocLimit ); if ( !VirtualAlloc( m_pCommitLimit, commitSize, VA_COMMIT_FLAGS, PAGE_READWRITE ) ) { Assert( 0 ); return NULL; } m_pCommitLimit = pNewCommitLimit; MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); #else Assert( 0 ); return NULL; #endif } memset( pResult, 0, bytes ); return pResult; }
bool CMemoryStack::Init( unsigned maxSize, unsigned commitSize, unsigned initialCommit, unsigned alignment ) { Assert( !m_pBase ); #ifdef _X360 m_bPhysical = false; #endif m_maxSize = maxSize; m_alignment = AlignValue( alignment, 4 ); Assert( m_alignment == alignment ); Assert( m_maxSize > 0 ); #if defined(_WIN32) if ( commitSize != 0 ) { m_commitSize = commitSize; } unsigned pageSize; #ifndef _X360 SYSTEM_INFO sysInfo; GetSystemInfo( &sysInfo ); Assert( !( sysInfo.dwPageSize & (sysInfo.dwPageSize-1)) ); pageSize = sysInfo.dwPageSize; #else pageSize = 64*1024; #endif if ( m_commitSize == 0 ) { m_commitSize = pageSize; } else { m_commitSize = AlignValue( m_commitSize, pageSize ); } m_maxSize = AlignValue( m_maxSize, m_commitSize ); Assert( m_maxSize % pageSize == 0 && m_commitSize % pageSize == 0 && m_commitSize <= m_maxSize ); m_pBase = (unsigned char *)VirtualAlloc( NULL, m_maxSize, VA_RESERVE_FLAGS, PAGE_NOACCESS ); Assert( m_pBase ); m_pCommitLimit = m_pNextAlloc = m_pBase; if ( initialCommit ) { initialCommit = AlignValue( initialCommit, m_commitSize ); Assert( initialCommit < m_maxSize ); if ( !VirtualAlloc( m_pCommitLimit, initialCommit, VA_COMMIT_FLAGS, PAGE_READWRITE ) ) return false; m_minCommit = initialCommit; m_pCommitLimit += initialCommit; MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); } #else m_pBase = (byte *)MemAlloc_AllocAligned( m_maxSize, alignment ? alignment : 1 ); m_pNextAlloc = m_pBase; m_pCommitLimit = m_pBase + m_maxSize; #endif m_pAllocLimit = m_pBase + m_maxSize; return ( m_pBase != NULL ); }