Game Development Reference
In-Depth Information
Allocate() is called from the Load() method when a resource is loaded. It calls
MakeRoom() if there isn
t enough room in the cache and updates the member vari-
able to keep track of all the allocated resources.
'
bool ResCache::MakeRoom(unsigned int size)
{
if (size > m_cacheSize)
{
return false;
}
// return null if there
s no possible way to allocate the memory
while (size > (m_cacheSize - m_allocated))
{
'
// The cache is empty, and there
'
s still not enough room.
if (m_lru.empty())
return false;
FreeOneResource();
}
return true;
}
After the initial sanity check, the while loop in MakeRoom() performs the work of
removing enough resources from the cache to load the new resource by calling
FreeOneResource() . If there
'
s already enough room, the loop is skipped.
void ResCache::FreeOneResource()
{
ResHandleList::iterator gonner = m_lru.end();
gonner--;
shared_ptr<ResHandle> handle = *gonner;
m_lru.pop_back();
m_resources.erase(handle->m_resource.m_name);
}
ResCache::FreeOneResource() removes the oldest resource and updates the
cache data members. Note that the memory used by the cache isn
'
t actually modified
here
s because any active shared_ptr<ResHandle> in use will need the bits
until it actually goes out of scope.
that
'
Search WWH ::




Custom Search