From 97a07326828b6cdabaa555eb8061ca116d9cea39 Mon Sep 17 00:00:00 2001 From: "wagic.the.homebrew@gmail.com" Date: Thu, 15 Oct 2009 13:58:35 +0000 Subject: [PATCH] Erwan - Fix issue 97. Please review. How about performance ? --- projects/mtg/include/WResourceManager.h | 2 +- projects/mtg/src/GameStateShop.cpp | 3 +- projects/mtg/src/WCachedResource.cpp | 1 + projects/mtg/src/WResourceManager.cpp | 64 +++++++------------------ 4 files changed, 20 insertions(+), 50 deletions(-) diff --git a/projects/mtg/include/WResourceManager.h b/projects/mtg/include/WResourceManager.h index c9c62e60a..d1c6fef4c 100644 --- a/projects/mtg/include/WResourceManager.h +++ b/projects/mtg/include/WResourceManager.h @@ -12,7 +12,7 @@ #define PSI_CACHE_SIZE 500000 // Size in bytes of the cahed particles #define TEXTURES_CACHE_MINSIZE 2000000 // Minimum size of the cache on the PSP. The program should complain if the cache ever gets smaller than this #define OPERATIONAL_SIZE 5000000 // Size required by Wagic for operational stuff. 3MB is not enough. The cache will usually try to take (Total Ram - Operational size) - +#define MIN_LINEAR_RAM 1000000 //Hard Limits. #define MAX_CACHE_OBJECTS 300 #define MAX_CACHE_ATTEMPTS 10 diff --git a/projects/mtg/src/GameStateShop.cpp b/projects/mtg/src/GameStateShop.cpp index 415513297..9814272df 100644 --- a/projects/mtg/src/GameStateShop.cpp +++ b/projects/mtg/src/GameStateShop.cpp @@ -30,7 +30,7 @@ void GameStateShop::Start() mStage = STAGE_SHOP_SHOP; - bgTexture = resources.RetrieveTexture("shop.jpg"); + bgTexture = resources.RetrieveTexture("shop.jpg",RETRIEVE_LOCK); //alternateRender doesn't lock, so lock our thumbnails for hgeDistort. altThumb[0] = resources.RetrieveTexture("artifact_thumb.jpg", RETRIEVE_LOCK); @@ -125,7 +125,6 @@ void GameStateShop::load(){ void GameStateShop::End() { JRenderer::GetInstance()->EnableVSync(false); - resources.Release(mBg); resources.Release(bgTexture); //Release alternate thumbnails. diff --git a/projects/mtg/src/WCachedResource.cpp b/projects/mtg/src/WCachedResource.cpp index 3352aeb3f..700403ee0 100644 --- a/projects/mtg/src/WCachedResource.cpp +++ b/projects/mtg/src/WCachedResource.cpp @@ -287,6 +287,7 @@ bool WCachedTexture::Attempt(string filename, int submode, int & error){ //Failure. if(!texture){ + error = CACHE_ERROR_BAD; if(!fileExists(realname.c_str())) error = CACHE_ERROR_404; return false; diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index afe184b70..0a7012a57 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -971,53 +971,18 @@ cacheItem* WCache::AttemptNew(string filename, int submo mError = CACHE_ERROR_NONE; - for(int attempts = 0; attempts < MAX_CACHE_ATTEMPTS;attempts++) - { - //We use try/catch so any memory alloc'd in Attempt isn't lost. - try{ - //If we don't get a good item, remove oldest cache and continue trying. - if(!item->Attempt(filename,submode,mError) || !item->isGood()) { - //No such file. Fail on first try. - if(mError == CACHE_ERROR_404){ - SAFE_DELETE(item); - return NULL; - } - throw std::bad_alloc(); - } + if(!item->Attempt(filename,submode,mError) || !item->isGood()) { + //No such file. Fail + if(mError == CACHE_ERROR_404){ + SAFE_DELETE(item); + return NULL; } - catch(std::bad_alloc){ - RemoveOldest(); - } - - //Succeeded - if(item->isGood()) - break; - } - - //Still no result, so clear local cache, then try again. - if(!item->isGood()){ - ClearUnlocked(); - try{ - if(!item->Attempt(filename,submode,mError) || !item->isGood()) - throw std::bad_alloc(); - } - catch(std::bad_alloc){ - //Failed, so clear every cache we've got in prep for the next try. - resources.ClearUnlocked(); - } - if(!item->isGood()){ - try{ - if(!item->Attempt(filename,submode,mError) || !item->isGood()) - throw std::bad_alloc(); - } - catch(std::bad_alloc){ - //Complete failure. Trash this object and return NULL. - if(!item->isGood()){ - SAFE_DELETE(item); - mError = CACHE_ERROR_BAD; - return NULL; - } - } + //Probably not enough memory: cleanup and try again + Cleanup(); + if(!item->Attempt(filename,submode,mError) || !item->isGood()) { + SAFE_DELETE(item); + mError = CACHE_ERROR_BAD; + return NULL; } } @@ -1214,7 +1179,12 @@ bool WCache::Cleanup(){ RemoveMiss(); } - while (cacheItems > MAX_CACHE_OBJECTS || cacheItems > maxCached || cacheSize > maxCacheSize ){ + while (cacheItems > MAX_CACHE_OBJECTS || cacheItems > maxCached || cacheSize > maxCacheSize +#if defined WIN32 || defined LINUX +#else + || ramAvailableLineareMax() < MIN_LINEAR_RAM +#endif + ){ if (!RemoveOldest()) return false; }