📄 surfheapv.cpp
字号:
//**********************************************************************
//
// Filename: surfheapv.cpp
//
// Description:
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to
// use this source code. For a copy of the EULA, please see the
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved
//
//**********************************************************************
#include "precomp.h"
#define ZONE_SURFACEHEAP 0
DWORD SurfaceHeapV::Available()
{
SurfaceHeapV *pNode;
DWORD available = 0;
for( pNode = this; pNode; pNode = pNode->m_pNext )
if( !pNode->m_fUsed )
available += pNode->m_nSize;
return available;
}
SurfaceHeapV::SurfaceHeapV( DWORD size, ADDRESS base, SurfaceHeapV *pNext, SurfaceHeapV *pPrev )
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::SurfaceHeapV size %d address 0x%08x pPrev %08x pNext %08x this %08x\r\n"),
size, base, pPrev, pNext, this ));
if( m_pNext = pNext ) // deliberate assign
m_pNext->m_pPrev = this;
if( m_pPrev = pPrev ) // deliberate assign
m_pPrev->m_pNext = this;
m_pStart = base;
m_nSize = size;
m_fUsed = 0;
}
SurfaceHeapV::~SurfaceHeapV()
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::~SurfaceHeapV 0x%08x (size=%d)\r\n"),this,m_nSize));
if( !m_pPrev )
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("Deleting heap!\r\n")));
// if this is the base, then we are deleting the heap,
SurfaceHeapV *pLast = this;
while( pLast->m_pNext )
pLast = pLast->m_pNext;
while( pLast != this )
{
pLast = pLast->m_pPrev;
pLast->m_pNext->m_pPrev = (SurfaceHeapV *)NULL; // prevent SurfaceHeapV::~SurfaceHeapV from doing anything
delete pLast->m_pNext;
pLast->m_pNext = (SurfaceHeapV *)NULL;
}
}
else
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("Deleting node only\r\n")));
// otherwise, we are just freeing this node
m_pPrev->m_nSize += m_nSize;
m_pPrev->m_pNext = m_pNext;
if( m_pNext )
m_pNext->m_pPrev = m_pPrev;
}
}
SurfaceHeapV *SurfaceHeapV::Alloc( DWORD size )
{
SurfaceHeapV *pNode = this;
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::Alloc(%d)\r\n"),size));
while( pNode && ( ( pNode->m_fUsed ) || ( pNode->m_nSize < size ) ) )
pNode = pNode->m_pNext;
if( pNode && ( pNode->m_nSize > size ) )
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("Splitting %d byte node at 0x%08x\r\n"), pNode->m_nSize, pNode ));
// Ok, have to split into used & unused section
SurfaceHeapV *pFreeNode = new
SurfaceHeapV(
pNode->m_nSize - size, // size
(ADDRESS)(size + (unsigned long)pNode->m_pStart), // start
pNode->m_pNext, // next
pNode // prev
);
if( !pFreeNode )
{
pNode = (SurfaceHeapV *)NULL; // out of memory for new node
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("Failed to allocate new node\r\n")));
}
else
{
pNode->m_nSize = size;
}
}
if( pNode )
{
pNode->m_fUsed = 1;
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("Marking node at 0x%08x as used (offset = 0x08x)\r\n"),pNode, pNode->Address()));
}
return pNode;
}
void SurfaceHeapV::Free()
{
SurfaceHeapV *pMerge;
SurfaceHeapV *pNode = this;
pNode->m_fUsed = 0;
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::Free 0x%08x (size=%d)\r\n"),pNode,pNode->m_nSize));
pMerge = pNode->m_pPrev;
if( pMerge && !pMerge->m_fUsed )
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::Free - merging node %08x with prior node (%08x)\r\n"),
pNode, pMerge ));
delete pNode; // Merge pNode with prior node
pNode = pMerge;
}
pMerge = pNode->m_pNext;
if( pMerge && !pMerge->m_fUsed )
{
DEBUGMSG(ZONE_SURFACEHEAP,(TEXT("SurfaceHeapV::Free - merging %08x with subsequent node (%08x)\r\n"),
pNode, pMerge ));
delete pMerge; // Merge pNode with subsequent node
}
}
DWORD SurfaceHeapV::Size()
{
// this shouldn't be called too often, - just walk through accumulating total size;
SurfaceHeapV *pNode;
DWORD size = 0;
for( pNode=this; pNode; pNode = pNode->m_pNext )
size += pNode->m_nSize;
return size;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -