// ------------------------------------------------------------------------------------------------------- void* AllocatorMT::Alloc( SizeT size ) { UInt32 threadID = (UInt32)GetCurrentThreadId(); MemPool* pool = FindPool(threadID); if (pool == 0) { CreatePool(threadID); pool = FindPool(threadID); A_ASSERT(pool != 0, "Unable to create pool for this thread!"); } return pool->Alloc(size); }
void *CSmallBlockHeap::Realloc( void *p, size_t nBytes ) { if ( nBytes == 0) { nBytes = 1; } CSmallBlockPool *pOldPool = FindPool( p ); CSmallBlockPool *pNewPool = ( ShouldUse( nBytes ) ) ? FindPool( nBytes ) : NULL; if ( pOldPool == pNewPool ) { return p; } void *pNewBlock = NULL; if ( pNewPool ) { pNewBlock = pNewPool->Alloc(); if ( !pNewBlock ) { if ( s_StdMemAlloc.CallAllocFailHandler( nBytes ) >= nBytes ) { pNewBlock = pNewPool->Alloc(); } } } if ( !pNewBlock ) { pNewBlock = malloc( nBytes ); if ( !pNewBlock ) { s_StdMemAlloc.SetCRTAllocFailed( nBytes ); } } if ( pNewBlock ) { int nBytesCopy = min( nBytes, pOldPool->GetBlockSize() ); memcpy( pNewBlock, p, nBytesCopy ); } pOldPool->Free( p ); return pNewBlock; }
void unsynchronized_pool_resource::do_deallocate(void * p, size_type bytes, size_type alignment) noexcept { auto finalSize = Internal::GetFinalSize(bytes, alignment); if (finalSize > maxBlockSize_) return poolManager_.Deallocate(p); const auto poolIndex = FindPool(bytes); pools_[poolIndex].Deallocate(p); }
void *CSmallBlockHeap::Alloc( size_t nBytes ) { if ( nBytes == 0) { nBytes = 1; } Assert( ShouldUse( nBytes ) ); CSmallBlockPool *pPool = FindPool( nBytes ); void *p = pPool->Alloc(); if ( p ) { return p; } if ( s_StdMemAlloc.CallAllocFailHandler( nBytes ) >= nBytes ) { p = pPool->Alloc(); if ( p ) { return p; } } void *pRet = malloc( nBytes ); if ( !pRet ) { s_StdMemAlloc.SetCRTAllocFailed( nBytes ); } return pRet; }
// ------------------------------------------------------------------------------------------------------- Bool AllocatorMT::Free( void* ptr ) { UInt32 threadID = (UInt32)GetCurrentThreadId(); MemPool* pool = FindPool(threadID); A_ASSERT(pool != 0, "Try to free a pointer which not in this thread!"); return pool->Free(ptr); }
void* unsynchronized_pool_resource::do_allocate(size_type bytes, size_type alignment) { const auto finalSize = Internal::GetFinalSize(bytes, alignment); if (finalSize > maxBlockSize_) { return poolManager_.Allocate(bytes, alignment); } const auto poolIndex = FindPool(bytes); return pools_[poolIndex].Allocate(bytes, alignment); }
void *CX360SmallBlockHeap::Realloc( void *p, size_t nBytes ) { if ( nBytes == 0) { nBytes = 1; } CX360SmallBlockPool *pOldPool = FindPool( p ); CX360SmallBlockPool *pNewPool = ( ShouldUse( nBytes ) ) ? FindPool( nBytes ) : NULL; if ( pOldPool == pNewPool ) { return p; } void *pNewBlock = NULL; if ( pNewPool ) { pNewBlock = pNewPool->Alloc(); if ( !pNewBlock ) { pNewBlock = GetStandardSBH()->Alloc( nBytes ); } } if ( !pNewBlock ) { pNewBlock = malloc( nBytes ); } if ( pNewBlock ) { int nBytesCopy = min( nBytes, pOldPool->GetBlockSize() ); memcpy( pNewBlock, p, nBytesCopy ); } pOldPool->Free( p ); return pNewBlock; }
void *CX360SmallBlockHeap::Alloc( size_t nBytes ) { if ( nBytes == 0) { nBytes = 1; } Assert( ShouldUse( nBytes ) ); CX360SmallBlockPool *pPool = FindPool( nBytes ); void *p = pPool->Alloc(); if ( p ) { return p; } return GetStandardSBH()->Alloc( nBytes ); }
// ------------------------------------------------------------------------------------------------------- Bool AllocatorMT::CreatePool( UInt32 threadID ) { EnterCriticalSection(&mCriticalSection); Bool result = 0; if(FindPool(threadID) == 0) { A_ASSERT(mPoolCount < MaxThreadCount, "Try to create pools over max number limits"); mThreadIDs[mPoolCount] = threadID; mMemPools[mPoolCount] = new MemPool(); ++mPoolCount; result = true; } else { result = false; } LeaveCriticalSection(&mCriticalSection); return result; }
size_t CX360SmallBlockHeap::GetSize( void *p ) { CX360SmallBlockPool *pPool = FindPool( p ); return pPool->GetBlockSize(); }
void CX360SmallBlockHeap::Free( void *p ) { CX360SmallBlockPool *pPool = FindPool( p ); pPool->Free( p ); }
bool CX360SmallBlockPool::IsOwner( void *p ) { return ( FindPool( p ) == this ); }