Jeck - Card ID collisions, cache fixes, cache deleted pooling.

* mtgid now defaults to 0.
 * TextScroller will not update when empty.
 * Cache now moves WCachedResources we're finished with to a garbage pool for later use (to reduce memory fragmentation). 
 * Demo still crashes... but I'm thinking that has to do with fragmentation, not a leak?
This commit is contained in:
wagic.jeck
2009-09-19 01:48:42 +00:00
parent 5d57693d31
commit 4b8d344bcd
11 changed files with 893 additions and 738 deletions
+2
View File
@@ -213,6 +213,7 @@ power=2
toughness=2 toughness=2
[/card] [/card]
[card] [card]
id=152659
Name=Shinewend Name=Shinewend
Mana={1}{W} Mana={1}{W}
Type=Creature Type=Creature
@@ -226,6 +227,7 @@ toughness=0
Rarity=C Rarity=C
[/card] [/card]
[card] [card]
id=152704
Name=Stingmoggie Name=Stingmoggie
Mana={3}{R} Mana={3}{R}
Type=Creature Type=Creature
+1 -1
View File
@@ -163,7 +163,7 @@ color=Red
[/card] [/card]
[card] [card]
text={T}:Prodigal Pyromancer deals 1 damage to target creature or player. text={T}:Prodigal Pyromancer deals 1 damage to target creature or player.
id=134752 id=122338
name=Prodigal Pyromancer name=Prodigal Pyromancer
rarity=C rarity=C
type=Creature type=Creature
+1
View File
@@ -20,6 +20,7 @@ class GameStateMenu: public GameState, public JGuiListener
JTexture * bgTexture; JTexture * bgTexture;
JTexture * movingWTexture; JTexture * movingWTexture;
JQuad * mBg; JQuad * mBg;
JQuad * mSplash;
JQuad * mMovingW; JQuad * mMovingW;
float mCreditsYPos; float mCreditsYPos;
int currentState; int currentState;
+8
View File
@@ -16,6 +16,7 @@ public:
WResource(); WResource();
virtual ~WResource(); virtual ~WResource();
virtual void Trash()=0; //Delete the cacheActual.
virtual void Nullify()=0; //For when our size is 0, so we don't free anything by mistake. virtual void Nullify()=0; //For when our size is 0, so we don't free anything by mistake.
virtual unsigned long size()=0; //Size of cached item in bytes. virtual unsigned long size()=0; //Size of cached item in bytes.
virtual bool isGood()=0; //Return true if this has data. virtual bool isGood()=0; //Return true if this has data.
@@ -38,6 +39,8 @@ public:
friend class WResourceManager; friend class WResourceManager;
template<class cacheItem,class cacheActual> friend class WCache; template<class cacheItem,class cacheActual> friend class WCache;
virtual ~WCachedResource() {};
virtual void Refresh(string filename)=0; //Basically calls Attempt(filename) and remaps in situ. virtual void Refresh(string filename)=0; //Basically calls Attempt(filename) and remaps in situ.
virtual bool Attempt(string filename, int submode, int & error)=0; //Returns true if we've loaded our data and isGood(). virtual bool Attempt(string filename, int submode, int & error)=0; //Returns true if we've loaded our data and isGood().
}; };
@@ -48,6 +51,7 @@ public:
WTrackedQuad(string _resname); WTrackedQuad(string _resname);
~WTrackedQuad(); ~WTrackedQuad();
void Nullify(); void Nullify();
void Trash();
unsigned long size(); unsigned long size();
bool isGood(); bool isGood();
@@ -73,6 +77,7 @@ public:
bool compare(JTexture * t) {return (t == texture);}; bool compare(JTexture * t) {return (t == texture);};
void Nullify(); void Nullify();
void Trash();
JTexture * Actual(); //Return this texture as is. Does not make a new one. JTexture * Actual(); //Return this texture as is. Does not make a new one.
JQuad * GetQuad(string resname); JQuad * GetQuad(string resname);
@@ -86,6 +91,7 @@ protected:
JTexture * texture; JTexture * texture;
bool bVRAM; bool bVRAM;
vector<WTrackedQuad*> trackedQuads; vector<WTrackedQuad*> trackedQuads;
static vector<WTrackedQuad*> garbageTQs;
}; };
class WCachedParticles: public WCachedResource{ class WCachedParticles: public WCachedResource{
@@ -96,6 +102,7 @@ public:
~WCachedParticles(); ~WCachedParticles();
void Nullify(); void Nullify();
void Trash();
void Refresh(string filename); void Refresh(string filename);
unsigned long size(); unsigned long size();
bool isGood(); bool isGood();
@@ -114,6 +121,7 @@ public:
WCachedSample(); WCachedSample();
~WCachedSample(); ~WCachedSample();
void Nullify(); void Nullify();
void Trash();
bool compare(JSample * s) {return (s == sample);}; bool compare(JSample * s) {return (s == sample);};
unsigned long size(); unsigned long size();
bool isGood(); bool isGood();
+8 -3
View File
@@ -19,10 +19,11 @@
//Hard Limits. //Hard Limits.
#define MAX_CACHE_OBJECTS 400 #define MAX_CACHE_OBJECTS HUGE_CACHE_ITEMS
#define MAX_CACHE_ATTEMPTS 10 #define MAX_CACHE_ATTEMPTS 10
#define MAX_CACHE_MISSES 200 #define MAX_CACHE_MISSES 200
#define MAX_CACHED_SAMPLES 0 #define MAX_CACHED_SAMPLES 0
#define MAX_CACHE_GARBAGE 10
enum ENUM_WRES_INFO{ enum ENUM_WRES_INFO{
WRES_UNLOCKED = 0, //Resource is unlocked. WRES_UNLOCKED = 0, //Resource is unlocked.
@@ -61,7 +62,8 @@ enum ENUM_CACHE_ERROR{
CACHE_ERROR_NONE = 0, CACHE_ERROR_NONE = 0,
CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE, CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE,
CACHE_ERROR_404, CACHE_ERROR_404,
CACHE_ERROR_BAD, CACHE_ERROR_BAD, //Something went wrong with item->attempt()
CACHE_ERROR_BAD_ALLOC, //Couldn't allocate item
CACHE_ERROR_LOST, CACHE_ERROR_LOST,
CACHE_ERROR_NOT_MANAGED, CACHE_ERROR_NOT_MANAGED,
}; };
@@ -89,15 +91,17 @@ public:
protected: protected:
bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care. bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care.
bool Delete(cacheItem * item); //Call SAFE_DELETE on cacheItem. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!) bool Delete(cacheItem * item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
cacheItem* Get(string id, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve. cacheItem* Get(string id, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve.
cacheItem* AttemptNew(string filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails. cacheItem* AttemptNew(string filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails.
cacheItem* Recycle(); //Returns a cache item from the trash.
string makeID(string filename, int submode); //Makes an ID appropriate to the submode. string makeID(string filename, int submode); //Makes an ID appropriate to the submode.
string makeFilename(string id, int submode); //Makes a filename from an ID. string makeFilename(string id, int submode); //Makes a filename from an ID.
map<string,cacheItem*> cache; map<string,cacheItem*> cache;
map<string,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate. map<string,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate.
vector<cacheItem*> garbage; //Garbage collection.
unsigned long totalSize; unsigned long totalSize;
unsigned long cacheSize; unsigned long cacheSize;
@@ -138,6 +142,7 @@ public:
void Release(JQuad * quad); void Release(JQuad * quad);
void Release(JSample * sample); void Release(JSample * sample);
bool RemoveOldest();
bool Cleanup(); //Force a cleanup. Return false if nothing removed. bool Cleanup(); //Force a cleanup. Return false if nothing removed.
void ClearMisses(); //Remove all cache misses. void ClearMisses(); //Remove all cache misses.
+8 -3
View File
@@ -64,6 +64,9 @@ GameStateMenu::GameStateMenu(GameApp* parent): GameState(parent)
mGuiController = NULL; mGuiController = NULL;
subMenuController = NULL; subMenuController = NULL;
gameTypeMenu = NULL; gameTypeMenu = NULL;
mSplash = NULL;
mBg = NULL;
mMovingW = NULL;
//bgMusic = NULL; //bgMusic = NULL;
timeIndex = 0; timeIndex = 0;
angleMultiplier = MIN_ANGLE_MULTIPLIER; angleMultiplier = MIN_ANGLE_MULTIPLIER;
@@ -315,6 +318,7 @@ void GameStateMenu::Update(float dt)
std::ifstream file(options.profileFile(PLAYER_COLLECTION,"",false).c_str()); std::ifstream file(options.profileFile(PLAYER_COLLECTION,"",false).c_str());
if(file){ if(file){
file.close(); file.close();
resources.Release(mSplash);
currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_NONE; currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_NONE;
}else{ }else{
currentState = MENU_STATE_MAJOR_FIRST_TIME | MENU_STATE_MINOR_NONE; currentState = MENU_STATE_MAJOR_FIRST_TIME | MENU_STATE_MINOR_NONE;
@@ -414,9 +418,10 @@ void GameStateMenu::Render()
renderer->ClearScreen(ARGB(0,0,0,0)); renderer->ClearScreen(ARGB(0,0,0,0));
JLBFont * mFont = resources.GetJLBFont(Constants::MENU_FONT); JLBFont * mFont = resources.GetJLBFont(Constants::MENU_FONT);
if ((currentState & MENU_STATE_MAJOR) == MENU_STATE_MAJOR_LOADING_CARDS){ if ((currentState & MENU_STATE_MAJOR) == MENU_STATE_MAJOR_LOADING_CARDS){
JQuad* splashQuad = resources.RetrieveTempQuad("splash.jpg"); if(!mSplash)
if (splashQuad){ mSplash = resources.RetrieveQuad("splash.jpg");
renderer->RenderQuad(splashQuad,0,0); if (mSplash){
renderer->RenderQuad(mSplash,0,0);
}else{ }else{
char text[512]; char text[512];
sprintf(text, _("LOADING SET: %s").c_str(), mCurrentSetName); sprintf(text, _("LOADING SET: %s").c_str(), mCurrentSetName);
+1
View File
@@ -69,6 +69,7 @@ int MTGCard::init(){
colors[i] = 0; colors[i] = 0;
} }
setId = 0; setId = 0;
mtgid = 0;
magicText = ""; magicText = "";
spellTargetType = ""; spellTargetType = "";
alias = 0; alias = 0;
+1 -1
View File
@@ -279,7 +279,7 @@ int MTGAllCards::readConfLine(std::ifstream &file, int set_id){
char outBuf[4096]; char outBuf[4096];
sprintf(outBuf,"warning, card id collision! : %i - %s\n", newId, tempCard->name.c_str()); sprintf(outBuf,"warning, card id collision! : %i - %s\n", newId, tempCard->name.c_str());
OutputDebugString (outBuf); OutputDebugString (outBuf);
delete tempCard; SAFE_DELETE(tempCard);
}else{ }else{
ids.push_back(newId); ids.push_back(newId);
collection[newId] = tempCard; collection[newId] = tempCard;
+2
View File
@@ -32,6 +32,8 @@ void TextScroller::Reset(){
} }
void TextScroller::Update(float dt){ void TextScroller::Update(float dt){
if(!strings.size())
return;
start+=mSpeed*dt; start+=mSpeed*dt;
if (start > mFont->GetStringWidth(mText.c_str())){ if (start > mFont->GetStringWidth(mText.c_str())){
start = -mWidth; start = -mWidth;
+46 -2
View File
@@ -59,6 +59,8 @@ void WResource::hit(){
lastTime = resources.nowTime(); lastTime = resources.nowTime();
} }
//WCachedTexture //WCachedTexture
vector<WTrackedQuad*> WCachedTexture::garbageTQs;
WCachedTexture::WCachedTexture(){ WCachedTexture::WCachedTexture(){
#ifdef DEBUG_CACHE #ifdef DEBUG_CACHE
OutputDebugString("Cached texture created.\n"); OutputDebugString("Cached texture created.\n");
@@ -122,7 +124,13 @@ bool WCachedTexture::ReleaseQuad(JQuad* quad){
tq->unlock(); tq->unlock();
if(!tq->isLocked()){ if(!tq->isLocked()){
SAFE_DELETE(tq); if(WCachedTexture::garbageTQs.size() < MAX_CACHE_GARBAGE){
tq->Trash();
garbageTQs.push_back(tq);
}
else
SAFE_DELETE(tq);
trackedQuads.erase(it); trackedQuads.erase(it);
} }
@@ -157,7 +165,14 @@ WTrackedQuad * WCachedTexture::GetTrackedQuad(float offX, float offY, float widt
if(tq == NULL){ if(tq == NULL){
allocated = true; allocated = true;
tq = NEW WTrackedQuad(resname); vector<WTrackedQuad*>::iterator gtq = WCachedTexture::garbageTQs.begin();
if(gtq != WCachedTexture::garbageTQs.end())
{
tq = *gtq;
garbageTQs.erase(gtq);
}
else
tq = NEW WTrackedQuad(resname);
} }
if(tq == NULL) if(tq == NULL)
@@ -314,6 +329,22 @@ void WCachedTexture::Nullify(){
if(texture) if(texture)
texture = NULL; texture = NULL;
} }
void WCachedTexture::Trash(){
SAFE_DELETE(texture);
vector<WTrackedQuad*>::iterator it;
WTrackedQuad * tq = NULL;
for(it=trackedQuads.begin();it!=trackedQuads.end();it++){
tq = (*it);
if(WCachedTexture::garbageTQs.size() > MAX_CACHE_GARBAGE)
SAFE_DELETE(tq);
else{
WCachedTexture::garbageTQs.push_back(tq);
}
}
trackedQuads.clear();
}
//WCachedSample //WCachedSample
void WCachedSample::Nullify(){ void WCachedSample::Nullify(){
@@ -322,6 +353,10 @@ void WCachedSample::Nullify(){
} }
} }
void WCachedSample::Trash(){
SAFE_DELETE(sample);
}
WCachedSample::WCachedSample(){ WCachedSample::WCachedSample(){
#ifdef DEBUG_CACHE #ifdef DEBUG_CACHE
OutputDebugString("Cached sample created.\n"); OutputDebugString("Cached sample created.\n");
@@ -452,11 +487,20 @@ void WCachedParticles::Nullify(){
particles = NULL; particles = NULL;
} }
void WCachedParticles::Trash(){
SAFE_DELETE(particles);
}
//WTrackedQuad //WTrackedQuad
void WTrackedQuad::Nullify() { void WTrackedQuad::Nullify() {
quad = NULL; quad = NULL;
} }
void WTrackedQuad::Trash(){
resname.clear();
SAFE_DELETE(quad);
}
#if defined DEBUG_CACHE #if defined DEBUG_CACHE
int WTrackedQuad::totalTracked = 0; int WTrackedQuad::totalTracked = 0;
#endif #endif
+133 -46
View File
@@ -10,9 +10,30 @@
#include "../include/WResourceManager.h" #include "../include/WResourceManager.h"
WResourceManager resources; WResourceManager resources;
unsigned int vTime = 0; unsigned int vTime = 0;
void handle_new_failure(){
OutputDebugString("NEW failed. Attempting to clear cache.");
#ifdef DEBUG_CACHE
resources.debugMessage = "Emergency cache cleanup!";
#endif
if(!resources.RemoveOldest()){
OutputDebugString("Nothing to clear from cache. Abort.");
abort();
}
}
bool WResourceManager::RemoveOldest(){
if(sampleWCache.RemoveOldest())
return true;
if(textureWCache.RemoveOldest())
return true;
if(psiWCache.RemoveOldest())
return true;
return false;
}
//WResourceManager //WResourceManager
void WResourceManager::DebugRender(){ void WResourceManager::DebugRender(){
JRenderer* renderer = JRenderer::GetInstance(); JRenderer* renderer = JRenderer::GetInstance();
@@ -168,18 +189,27 @@ WResourceManager::~WResourceManager(){
SAFE_DELETE(wm); SAFE_DELETE(wm);
} }
managedQuads.clear(); managedQuads.clear();
//Remove all our reserved WTrackedQuads from WCachedTexture
vector<WTrackedQuad*>::iterator g;
for(g=WCachedTexture::garbageTQs.begin();g!=WCachedTexture::garbageTQs.end();g++){
WTrackedQuad * tq = *g;
SAFE_DELETE(tq);
}
LOG("==Successfully Destroyed WResourceManager=="); LOG("==Successfully Destroyed WResourceManager==");
} }
JQuad * WResourceManager::RetrieveCard(MTGCard * card, int style, int submode){ JQuad * WResourceManager::RetrieveCard(MTGCard * card, int style, int submode){
//Cards are never, ever resource managed, so just check cache. //Cards are never, ever resource managed, so just check cache.
if(!card)
return NULL;
submode = submode | TEXTURE_SUB_CARD; submode = submode | TEXTURE_SUB_CARD;
string filename = card->getSetName(); string filename = card->getSetName();
filename += "/"; filename += "/";
filename += card->getImageName(); filename += card->getImageName();
JQuad * jq = RetrieveQuad(filename,0,0,0,0,filename,style,submode|TEXTURE_SUB_5551); JQuad * jq = RetrieveQuad(filename,0,0,0,0,"",style,submode|TEXTURE_SUB_5551);
if(jq){ if(jq){
jq->SetHotSpot(jq->mTex->mWidth / 2, jq->mTex->mHeight / 2); jq->SetHotSpot(jq->mTex->mWidth / 2, jq->mTex->mHeight / 2);
@@ -335,9 +365,8 @@ void WResourceManager::Release(JQuad * quad){
break; break;
} }
//Releasing a quad doesn't release the associated texture-- it might be needed later. if(it != textureWCache.cache.end() && it->second)
//if(it != textureWCache.cache.end() && it->second) textureWCache.RemoveItem(it->second,false); //won't remove locked.
// textureWCache.RemoveItem(it->second,false); //won't remove locked.
} }
void WResourceManager::ClearMisses(){ void WResourceManager::ClearMisses(){
@@ -400,6 +429,9 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style, int sub
case CACHE_ERROR_404: case CACHE_ERROR_404:
debugMessage = "File not found: "; debugMessage = "File not found: ";
break; break;
case CACHE_ERROR_BAD_ALLOC:
debugMessage = "Out of memory: ";
break;
case CACHE_ERROR_BAD: case CACHE_ERROR_BAD:
debugMessage = "Cache bad: "; debugMessage = "Cache bad: ";
break; break;
@@ -892,10 +924,6 @@ bool WCache<cacheItem, cacheActual>::RemoveOldest(){
} }
if(oldest != cache.end() && oldest->second && !oldest->second->isLocked()){ if(oldest != cache.end() && oldest->second && !oldest->second->isLocked()){
unsigned long isize = oldest->second->size();
cacheSize -= isize;
totalSize -= isize;
cacheItems--;
#if defined DEBUG_CACHE #if defined DEBUG_CACHE
lastExpired = oldest->first; lastExpired = oldest->first;
#endif #endif
@@ -915,13 +943,8 @@ void WCache<cacheItem, cacheActual>::Clear(){
next = it; next = it;
next++; next++;
if(it->second){ if(it->second)
unsigned long isize = it->second->size();
totalSize -= isize;
cacheSize -= isize;
Delete(it->second); Delete(it->second);
cacheItems--;
}
cache.erase(it); cache.erase(it);
} }
for(it = managed.begin(); it != managed.end();it=next){ for(it = managed.begin(); it != managed.end();it=next){
@@ -942,11 +965,7 @@ void WCache<cacheItem, cacheActual>::ClearUnlocked(){
next++; next++;
if(it->second && !it->second->isLocked()){ if(it->second && !it->second->isLocked()){
unsigned long isize = it->second->size();
totalSize -= isize;
cacheSize -= isize;
Delete(it->second); Delete(it->second);
cacheItems--;
cache.erase(it); cache.erase(it);
} }
else if(!it->second){ else if(!it->second){
@@ -983,6 +1002,19 @@ void WCache<cacheItem, cacheActual>::Resize(unsigned long size, int items){
else else
maxCached = items; maxCached = items;
} }
template <class cacheItem, class cacheActual>
cacheItem* WCache<cacheItem, cacheActual>::Recycle(){
typename vector<cacheItem*>::iterator it = garbage.begin();
if(it == garbage.end())
return NULL;
cacheItem * item = (*it);
garbage.erase(it);
return item;
}
template <class cacheItem, class cacheActual> template <class cacheItem, class cacheActual>
cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submode){ cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submode){
if(submode & CACHE_EXISTING){ //Should never get this far. if(submode & CACHE_EXISTING){ //Should never get this far.
@@ -990,24 +1022,46 @@ cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submo
return NULL; return NULL;
} }
cacheItem* item = NEW cacheItem; cacheItem* item = NULL;
mError = CACHE_ERROR_NONE; item = Recycle();
if(item == NULL){
try{
item = NEW cacheItem;
mError = CACHE_ERROR_NONE;
}
catch(std::bad_alloc){
SAFE_DELETE(item);
}
}
if(item == NULL || !item->Attempt(filename,submode,mError)){ if(item == NULL || !item->Attempt(filename,submode,mError)){
//No such file. Fail. //No such file. Fail.
if(item && mError == CACHE_ERROR_404){ if(item && mError == CACHE_ERROR_404){
Delete(item); if(garbage.size() < MAX_CACHE_GARBAGE){
item->Trash();
garbage.push_back(item);
}
else
SAFE_DELETE(item);
return NULL; return NULL;
} }
for(int attempt=0;attempt<10;attempt++){ for(int attempt=0;attempt<10;attempt++){
if(!RemoveOldest()) if(!RemoveOldest())
break; break;
if(!item) if(!item){
item = NEW cacheItem ; try{
item = NEW cacheItem;
}
catch(std::bad_alloc){
SAFE_DELETE(item);
continue;
}
}
if(item && item->Attempt(filename,submode,mError)) if(item && item->Attempt(filename,submode,mError))
break; break;
@@ -1024,14 +1078,29 @@ cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submo
//Worst cache scenerio. Clear every cache we've got. //Worst cache scenerio. Clear every cache we've got.
if(!item || !item->isGood()){ if(!item || !item->isGood()){
resources.ClearUnlocked(); resources.ClearUnlocked();
if(!item) item = NEW(cacheItem); if(!item){
try{
item = NEW(cacheItem);
}
catch(std::bad_alloc){
mError = CACHE_ERROR_BAD_ALLOC;
SAFE_DELETE(item);
return NULL;
}
}
item->Attempt(filename,submode,mError); item->Attempt(filename,submode,mError);
} }
} }
if(item && !item->isGood()){ if(item && !item->isGood()){
Delete(item); if(garbage.size() < MAX_CACHE_GARBAGE){
item->Trash();
garbage.push_back(item);
}
else
SAFE_DELETE(item);
mError = CACHE_ERROR_BAD; mError = CACHE_ERROR_BAD;
return NULL;
} }
else else
mError = CACHE_ERROR_NONE; mError = CACHE_ERROR_NONE;
@@ -1194,9 +1263,16 @@ cacheItem * WCache<cacheItem, cacheActual>::Get(string id, int style, int submod
//Space in cache, make new texture //Space in cache, make new texture
item = AttemptNew(id,submode); item = AttemptNew(id,submode);
//Couldn't make new item. //Couldn't make GOOD new item.
if(item && !item->isGood()) if(item && !item->isGood())
Delete(item); {
if(garbage.size() < MAX_CACHE_GARBAGE){
item->Trash();
garbage.push_back(item);
}
else
SAFE_DELETE(item);
}
} }
if(style == RETRIEVE_MANAGE){ if(style == RETRIEVE_MANAGE){
@@ -1216,7 +1292,7 @@ cacheItem * WCache<cacheItem, cacheActual>::Get(string id, int style, int submod
//Succeeded in making a new item. //Succeeded in making a new item.
if(item){ if(item){
unsigned long isize =item->size(); unsigned long isize = item->size();
totalSize += isize; totalSize += isize;
mError = CACHE_ERROR_NONE; mError = CACHE_ERROR_NONE;
@@ -1225,9 +1301,6 @@ cacheItem * WCache<cacheItem, cacheActual>::Get(string id, int style, int submod
cacheSize += isize; cacheSize += isize;
} }
item->lock();
Cleanup(); //Strictly enforce limits.
item->unlock();
return item; return item;
} }
@@ -1272,14 +1345,21 @@ WCache<cacheItem, cacheActual>::~WCache(){
if(!it->second) if(!it->second)
continue; continue;
Delete(it->second); //Delete(it->second);
SAFE_DELETE(it->second);
} }
for(it=managed.begin();it!=managed.end();it++){ for(it=managed.begin();it!=managed.end();it++){
if(!it->second) if(!it->second)
continue; continue;
Delete(it->second); //Delete(it->second);
SAFE_DELETE(it->second);
}
typename vector<cacheItem*>::iterator g;
for(g=garbage.begin();g!=garbage.end();g++){
SAFE_DELETE(*g);
} }
} }
@@ -1346,11 +1426,7 @@ bool WCache<cacheItem, cacheActual>::RemoveItem(cacheItem * item, bool force){
break; break;
} }
if(it != cache.end() && it->second && (force || !it->second->isLocked())){ if(it != cache.end() && it->second && (force || !it->second->isLocked())){
unsigned long isize = it->second->size();
totalSize -= isize;
cacheSize -= isize;
cacheItems--;
#if defined DEBUG_CACHE #if defined DEBUG_CACHE
lastRemoved = it->first; lastRemoved = it->first;
#endif #endif
@@ -1377,7 +1453,7 @@ bool WCache<cacheItem, cacheActual>::UnlinkCache(cacheItem * item){
it->second = NULL; it->second = NULL;
unsigned long isize = item->size(); unsigned long isize = item->size();
cacheSize-=isize; cacheSize -= isize;
cacheItems--; cacheItems--;
cache.erase(it); cache.erase(it);
return true; return true;
@@ -1398,7 +1474,22 @@ bool WCache<cacheItem, cacheActual>::Delete(cacheItem * item){
if(maxCached == 0) if(maxCached == 0)
item->Nullify(); item->Nullify();
SAFE_DELETE(item); unsigned long isize = item->size();
totalSize -= isize;
cacheSize -= isize;
#ifdef DEBUG_CACHE
if(cacheItems == 0)
OutputDebugString("cacheItems out of sync.\n");
#endif
cacheItems--;
if(garbage.size() > MAX_CACHE_GARBAGE)
SAFE_DELETE(item);
else{
item->Trash();
garbage.push_back(item);
}
return true; return true;
} }
@@ -1419,12 +1510,8 @@ bool WCache<cacheItem, cacheActual>::Release(cacheActual* actual){
if(it->second){ if(it->second){
it->second->unlock(); //Release one lock. it->second->unlock(); //Release one lock.
if(it->second->isLocked()) if(it->second->isLocked())
return true; //Still locked, won't delete. return true; //Still locked, won't delete, not technically a failure.
unsigned long isize = it->second->size();
totalSize -= isize;
cacheSize -= isize;
cacheItems--;
#if defined DEBUG_CACHE #if defined DEBUG_CACHE
lastReleased = it->first; lastReleased = it->first;
#endif #endif