Jeck - Cache is using map<> again. The implementation is cleaner and seems a little faster.

This commit is contained in:
wagic.jeck
2009-09-19 23:20:43 +00:00
parent 8cc9bff484
commit 1ba7f108b0
5 changed files with 418 additions and 423 deletions

View File

@@ -21,7 +21,6 @@ public:
virtual unsigned long size()=0; //Size of cached item in bytes.
virtual bool isGood()=0; //Return true if this has data.
virtual bool isLocked(); //Is the resource locked?
virtual bool isTrash(); //Is the resource locked?
virtual void lock(); //Lock it.
virtual void unlock(bool force = false); //Unlock it. Forcing a lock will also remove "permanent" status.
@@ -30,7 +29,6 @@ public:
void hit(); //Update resource's last used time.
protected:
string id; //Our lookup value.
int loadedMode; //What submode settings were we loaded with? (For refresh)
unsigned int lastTime; //When was the last time we were hit?
unsigned char locks; //Remember to unlock when we're done using locked stuff, or else this'll be useless.
@@ -112,7 +110,7 @@ public:
bool compare(hgeParticleSystemInfo * p) {return (p == particles);};
hgeParticleSystemInfo * Actual();
protected:
protected:
hgeParticleSystemInfo * particles;
};

View File

@@ -6,21 +6,17 @@
#include "MTGDeck.h"
#include "MTGCard.h"
#include "WCachedResource.h"
#include <list>
//Soft limits.
//For values higher than ~6000000, we run the danger of hitting our reserved space in deck editor.
#define HUGE_CACHE_LIMIT 6000000
#define LARGE_CACHE_LIMIT 4000000
#define SMALL_CACHE_LIMIT 2000000
#define HUGE_CACHE_LIMIT 6000000
#define HUGE_CACHE_ITEMS 200
#define LARGE_CACHE_LIMIT 4000000
#define LARGE_CACHE_ITEMS 150
#define SMALL_CACHE_LIMIT 2000000
#define SMALL_CACHE_ITEMS 100
//We keep a certain amount of space reserved for non-cache use.
//This value was chosen to guarantee space for image loading.
#define CACHE_SPACE_RESERVED (512*512*sizeof(PIXEL_TYPE))
//Hard Limits.
#define MAX_CACHE_OBJECTS HUGE_CACHE_ITEMS
@@ -34,7 +30,6 @@ enum ENUM_WRES_INFO{
WRES_MAX_LOCK = 250, //Maximum number of locks for a resource.
WRES_PERMANENT = 251, //Resource is permanent (ie, managed)
WRES_UNDERLOCKED = 252, //Resource was released too many times.
WRES_TRASH = 253, //Resource is trash, and can be recycled.
};
enum ENUM_RETRIEVE_STYLE{
@@ -65,13 +60,12 @@ enum ENUM_CACHE_SUBTYPE{
enum ENUM_CACHE_ERROR{
CACHE_ERROR_NONE = 0,
CACHE_ERROR_NOT_CACHED,
CACHE_ERROR_NOT_MANAGED,
CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE,
CACHE_ERROR_404,
CACHE_ERROR_BAD, //Something went wrong with item->attempt()
CACHE_ERROR_BAD, //Something went wrong with item->attempt()
CACHE_ERROR_BAD_ALLOC, //Couldn't allocate item
CACHE_ERROR_FULL, //Cache is at maxCached.
CACHE_ERROR_LOST,
CACHE_ERROR_NOT_MANAGED,
};
template <class cacheItem, class cacheActual>
@@ -95,21 +89,19 @@ public:
void Resize(unsigned long size, int items); //Sets new limits, then enforces them. Lock safe, so not a "hard limit".
protected:
bool RemoveItem(cacheItem* item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
bool Delete(cacheItem* item); //Garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
bool AttemptNew(cacheItem* item, int submode); //Attempts to load item, progressively clearing mCache if it fails.
cacheItem* Get(string id, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve. Guarenteed isGood().
cacheItem* Recycle(); //Returns a cache item from the trash, or (worst possible case) pops a new one onto mCache.
void RecordMiss(string miss);
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 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* 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 makeFilename(string id, int submode); //Makes a filename from an ID.
cacheItem mCached[MAX_CACHE_OBJECTS];
list<cacheItem> mManaged; //Cache and managed items are seperate to improve performance.
list<string> mMisses;
map<string,cacheItem*> cache;
map<string,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate.
vector<cacheItem*> garbage; //Garbage collection.
unsigned long totalSize;
unsigned long cacheSize;
@@ -120,11 +112,6 @@ protected:
unsigned int cacheItems;
int mError;
#if defined DEBUG_CACHE
string lastRemoved;
string lastReleased;
string lastExpired;
#endif
};
struct WManagedQuad {
@@ -145,8 +132,7 @@ public:
JQuad * RetrieveQuad(string filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL);
JQuad * RetrieveTempQuad(string filename);
hgeParticleSystemInfo * RetrievePSI(string filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
int RetrieveError(); //Returns the error from the last call to ANY retrieve function.
int RetrieveError();
void Release(JTexture * tex);
void Release(JQuad * quad);

View File

@@ -16,16 +16,13 @@ WResource::~WResource(){
return;
}
WResource::WResource(){
locks = WRES_TRASH;
locks = WRES_UNLOCKED;
lastTime = resources.nowTime();
loadedMode = 0;
}
bool WResource::isLocked(){
return (locks != WRES_UNLOCKED && locks != WRES_TRASH);
}
bool WResource::isTrash(){
return (locks == WRES_TRASH);
return (locks != WRES_UNLOCKED);
}
bool WResource::isPermanent(){
@@ -65,17 +62,10 @@ void WResource::hit(){
vector<WTrackedQuad*> WCachedTexture::garbageTQs;
WCachedTexture::WCachedTexture(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached texture created.\n");
#endif
texture = NULL;
}
WCachedTexture::~WCachedTexture(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached texture destroyed.\n");
#endif
if(texture)
SAFE_DELETE(texture);
@@ -108,12 +98,6 @@ bool WCachedTexture::isLocked(){
}
bool WCachedTexture::ReleaseQuad(JQuad* quad){
#ifdef DEBUG_CACHE
char buf[512];
sprintf(buf,"ReleaseQuad: %d.\n", (int) quad);
OutputDebugString(buf);
#endif
if(quad == NULL)
return false;
@@ -174,10 +158,8 @@ WTrackedQuad * WCachedTexture::GetTrackedQuad(float offX, float offY, float widt
tq = *gtq;
garbageTQs.erase(gtq);
}
else{
else
tq = NEW WTrackedQuad(resname);
tq->unlock(true);
}
}
if(tq == NULL)
@@ -249,9 +231,6 @@ unsigned long WCachedTexture::size(){
}
bool WCachedTexture::isGood(){
if(locks == WRES_TRASH)
return false;
if(!texture)
return false;
@@ -329,9 +308,6 @@ bool WCachedTexture::Attempt(string filename, int submode, int & error){
return false;
}
if(locks == WRES_TRASH)
locks = WRES_UNLOCKED;
error = CACHE_ERROR_NONE;
return true;
}
@@ -341,11 +317,6 @@ void WCachedTexture::Nullify(){
texture = NULL;
}
void WCachedTexture::Trash(){
id = "";
locks = WRES_TRASH;
#ifdef DEBUG_CACHE
OutputDebugString("WCachedTexture::Trash()\n");
#endif
SAFE_DELETE(texture);
vector<WTrackedQuad*>::iterator it;
@@ -371,25 +342,14 @@ void WCachedSample::Nullify(){
}
void WCachedSample::Trash(){
id = "";
locks = WRES_TRASH;
#ifdef DEBUG_CACHE
OutputDebugString("WCachedSample::Trash()\n");
#endif
SAFE_DELETE(sample);
}
WCachedSample::WCachedSample(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached sample created.\n");
#endif
sample = NULL;
}
WCachedSample::~WCachedSample(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached sample destroyed.\n");
#endif
SAFE_DELETE(sample);
}
@@ -409,9 +369,6 @@ unsigned long WCachedSample::size(){
}
bool WCachedSample::isGood(){
if(locks == WRES_TRASH)
return false;
if(!sample || !sample->mSample)
return false;
@@ -436,18 +393,12 @@ bool WCachedSample::Attempt(string filename, int submode, int & error){
return false;
}
if(locks == WRES_TRASH)
locks = WRES_UNLOCKED;
return true;
}
//WCachedParticles
bool WCachedParticles::isGood(){
if(locks == WRES_TRASH)
return false;
if(!particles)
return false;
return true;
@@ -493,10 +444,6 @@ bool WCachedParticles::Attempt(string filename, int submode, int & error){
particles->sprite=NULL;
error = CACHE_ERROR_NONE;
if(locks == WRES_TRASH)
locks = WRES_UNLOCKED;
return true;
}
@@ -505,15 +452,9 @@ hgeParticleSystemInfo * WCachedParticles::Actual(){
}
WCachedParticles::WCachedParticles(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached particles created.\n");
#endif
particles = NULL;
}
WCachedParticles::~WCachedParticles(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached particles destroyed.\n");
#endif
SAFE_DELETE(particles);
}
@@ -523,11 +464,6 @@ void WCachedParticles::Nullify(){
}
void WCachedParticles::Trash(){
id = "";
locks = WRES_TRASH;
#ifdef DEBUG_CACHE
OutputDebugString("WCachedParticles::Trash()\n");
#endif
SAFE_DELETE(particles);
}
@@ -537,11 +473,6 @@ void WTrackedQuad::Nullify() {
}
void WTrackedQuad::Trash(){
id = "";
locks = WRES_TRASH;
#ifdef DEBUG_CACHE
OutputDebugString("WTrackedQuad::Trash()\n");
#endif
resname.clear();
SAFE_DELETE(quad);
}
@@ -554,9 +485,6 @@ unsigned long WTrackedQuad::size() {
return sizeof(JQuad);
}
bool WTrackedQuad::isGood(){
if(locks == WRES_TRASH)
return false;
return (quad != NULL);
}
WTrackedQuad::WTrackedQuad(string _resname) {

File diff suppressed because it is too large Load Diff