Jeck - Cache is using map<> again. The implementation is cleaner and seems a little faster.
This commit is contained in:
Binary file not shown.
@@ -21,7 +21,6 @@ public:
|
|||||||
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.
|
||||||
virtual bool isLocked(); //Is the resource locked?
|
virtual bool isLocked(); //Is the resource locked?
|
||||||
virtual bool isTrash(); //Is the resource locked?
|
|
||||||
virtual void lock(); //Lock it.
|
virtual void lock(); //Lock it.
|
||||||
virtual void unlock(bool force = false); //Unlock it. Forcing a lock will also remove "permanent" status.
|
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.
|
void hit(); //Update resource's last used time.
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
string id; //Our lookup value.
|
|
||||||
int loadedMode; //What submode settings were we loaded with? (For refresh)
|
int loadedMode; //What submode settings were we loaded with? (For refresh)
|
||||||
unsigned int lastTime; //When was the last time we were hit?
|
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.
|
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);};
|
bool compare(hgeParticleSystemInfo * p) {return (p == particles);};
|
||||||
|
|
||||||
hgeParticleSystemInfo * Actual();
|
hgeParticleSystemInfo * Actual();
|
||||||
protected:
|
protected:
|
||||||
hgeParticleSystemInfo * particles;
|
hgeParticleSystemInfo * particles;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,21 +6,17 @@
|
|||||||
#include "MTGDeck.h"
|
#include "MTGDeck.h"
|
||||||
#include "MTGCard.h"
|
#include "MTGCard.h"
|
||||||
#include "WCachedResource.h"
|
#include "WCachedResource.h"
|
||||||
#include <list>
|
|
||||||
|
|
||||||
//Soft limits.
|
//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 HUGE_CACHE_LIMIT 6000000
|
|
||||||
#define LARGE_CACHE_LIMIT 4000000
|
|
||||||
#define SMALL_CACHE_LIMIT 2000000
|
|
||||||
|
|
||||||
#define HUGE_CACHE_ITEMS 200
|
#define HUGE_CACHE_ITEMS 200
|
||||||
|
|
||||||
|
#define LARGE_CACHE_LIMIT 4000000
|
||||||
#define LARGE_CACHE_ITEMS 150
|
#define LARGE_CACHE_ITEMS 150
|
||||||
|
|
||||||
|
#define SMALL_CACHE_LIMIT 2000000
|
||||||
#define SMALL_CACHE_ITEMS 100
|
#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.
|
//Hard Limits.
|
||||||
#define MAX_CACHE_OBJECTS HUGE_CACHE_ITEMS
|
#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_MAX_LOCK = 250, //Maximum number of locks for a resource.
|
||||||
WRES_PERMANENT = 251, //Resource is permanent (ie, managed)
|
WRES_PERMANENT = 251, //Resource is permanent (ie, managed)
|
||||||
WRES_UNDERLOCKED = 252, //Resource was released too many times.
|
WRES_UNDERLOCKED = 252, //Resource was released too many times.
|
||||||
WRES_TRASH = 253, //Resource is trash, and can be recycled.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ENUM_RETRIEVE_STYLE{
|
enum ENUM_RETRIEVE_STYLE{
|
||||||
@@ -65,13 +60,12 @@ enum ENUM_CACHE_SUBTYPE{
|
|||||||
|
|
||||||
enum ENUM_CACHE_ERROR{
|
enum ENUM_CACHE_ERROR{
|
||||||
CACHE_ERROR_NONE = 0,
|
CACHE_ERROR_NONE = 0,
|
||||||
CACHE_ERROR_NOT_CACHED,
|
CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE,
|
||||||
CACHE_ERROR_NOT_MANAGED,
|
|
||||||
CACHE_ERROR_404,
|
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_BAD_ALLOC, //Couldn't allocate item
|
||||||
CACHE_ERROR_FULL, //Cache is at maxCached.
|
|
||||||
CACHE_ERROR_LOST,
|
CACHE_ERROR_LOST,
|
||||||
|
CACHE_ERROR_NOT_MANAGED,
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class cacheItem, class cacheActual>
|
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".
|
void Resize(unsigned long size, int items); //Sets new limits, then enforces them. Lock safe, so not a "hard limit".
|
||||||
|
|
||||||
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 Delete(cacheItem* item); //Garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
|
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!)
|
||||||
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.
|
||||||
cacheItem* Get(string id, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve. Guarenteed isGood().
|
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, or (worst possible case) pops a new one onto mCache.
|
cacheItem* Recycle(); //Returns a cache item from the trash.
|
||||||
|
|
||||||
void RecordMiss(string miss);
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
cacheItem mCached[MAX_CACHE_OBJECTS];
|
map<string,cacheItem*> cache;
|
||||||
list<cacheItem> mManaged; //Cache and managed items are seperate to improve performance.
|
map<string,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate.
|
||||||
list<string> mMisses;
|
vector<cacheItem*> garbage; //Garbage collection.
|
||||||
unsigned long totalSize;
|
unsigned long totalSize;
|
||||||
unsigned long cacheSize;
|
unsigned long cacheSize;
|
||||||
|
|
||||||
@@ -120,11 +112,6 @@ protected:
|
|||||||
unsigned int cacheItems;
|
unsigned int cacheItems;
|
||||||
int mError;
|
int mError;
|
||||||
|
|
||||||
#if defined DEBUG_CACHE
|
|
||||||
string lastRemoved;
|
|
||||||
string lastReleased;
|
|
||||||
string lastExpired;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WManagedQuad {
|
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 * 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);
|
JQuad * RetrieveTempQuad(string filename);
|
||||||
hgeParticleSystemInfo * RetrievePSI(string filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
hgeParticleSystemInfo * RetrievePSI(string filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||||
|
int RetrieveError();
|
||||||
int RetrieveError(); //Returns the error from the last call to ANY retrieve function.
|
|
||||||
|
|
||||||
void Release(JTexture * tex);
|
void Release(JTexture * tex);
|
||||||
void Release(JQuad * quad);
|
void Release(JQuad * quad);
|
||||||
|
|||||||
@@ -16,16 +16,13 @@ WResource::~WResource(){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WResource::WResource(){
|
WResource::WResource(){
|
||||||
locks = WRES_TRASH;
|
locks = WRES_UNLOCKED;
|
||||||
lastTime = resources.nowTime();
|
lastTime = resources.nowTime();
|
||||||
loadedMode = 0;
|
loadedMode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WResource::isLocked(){
|
bool WResource::isLocked(){
|
||||||
return (locks != WRES_UNLOCKED && locks != WRES_TRASH);
|
return (locks != WRES_UNLOCKED);
|
||||||
}
|
|
||||||
bool WResource::isTrash(){
|
|
||||||
return (locks == WRES_TRASH);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WResource::isPermanent(){
|
bool WResource::isPermanent(){
|
||||||
@@ -65,17 +62,10 @@ void WResource::hit(){
|
|||||||
vector<WTrackedQuad*> WCachedTexture::garbageTQs;
|
vector<WTrackedQuad*> WCachedTexture::garbageTQs;
|
||||||
|
|
||||||
WCachedTexture::WCachedTexture(){
|
WCachedTexture::WCachedTexture(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached texture created.\n");
|
|
||||||
#endif
|
|
||||||
texture = NULL;
|
texture = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
WCachedTexture::~WCachedTexture(){
|
WCachedTexture::~WCachedTexture(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached texture destroyed.\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(texture)
|
if(texture)
|
||||||
SAFE_DELETE(texture);
|
SAFE_DELETE(texture);
|
||||||
|
|
||||||
@@ -108,12 +98,6 @@ bool WCachedTexture::isLocked(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool WCachedTexture::ReleaseQuad(JQuad* quad){
|
bool WCachedTexture::ReleaseQuad(JQuad* quad){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
char buf[512];
|
|
||||||
|
|
||||||
sprintf(buf,"ReleaseQuad: %d.\n", (int) quad);
|
|
||||||
OutputDebugString(buf);
|
|
||||||
#endif
|
|
||||||
if(quad == NULL)
|
if(quad == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -174,10 +158,8 @@ WTrackedQuad * WCachedTexture::GetTrackedQuad(float offX, float offY, float widt
|
|||||||
tq = *gtq;
|
tq = *gtq;
|
||||||
garbageTQs.erase(gtq);
|
garbageTQs.erase(gtq);
|
||||||
}
|
}
|
||||||
else{
|
else
|
||||||
tq = NEW WTrackedQuad(resname);
|
tq = NEW WTrackedQuad(resname);
|
||||||
tq->unlock(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tq == NULL)
|
if(tq == NULL)
|
||||||
@@ -249,9 +231,6 @@ unsigned long WCachedTexture::size(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool WCachedTexture::isGood(){
|
bool WCachedTexture::isGood(){
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if(!texture)
|
if(!texture)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -329,9 +308,6 @@ bool WCachedTexture::Attempt(string filename, int submode, int & error){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
locks = WRES_UNLOCKED;
|
|
||||||
|
|
||||||
error = CACHE_ERROR_NONE;
|
error = CACHE_ERROR_NONE;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -341,11 +317,6 @@ void WCachedTexture::Nullify(){
|
|||||||
texture = NULL;
|
texture = NULL;
|
||||||
}
|
}
|
||||||
void WCachedTexture::Trash(){
|
void WCachedTexture::Trash(){
|
||||||
id = "";
|
|
||||||
locks = WRES_TRASH;
|
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("WCachedTexture::Trash()\n");
|
|
||||||
#endif
|
|
||||||
SAFE_DELETE(texture);
|
SAFE_DELETE(texture);
|
||||||
|
|
||||||
vector<WTrackedQuad*>::iterator it;
|
vector<WTrackedQuad*>::iterator it;
|
||||||
@@ -371,25 +342,14 @@ void WCachedSample::Nullify(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WCachedSample::Trash(){
|
void WCachedSample::Trash(){
|
||||||
id = "";
|
|
||||||
locks = WRES_TRASH;
|
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("WCachedSample::Trash()\n");
|
|
||||||
#endif
|
|
||||||
SAFE_DELETE(sample);
|
SAFE_DELETE(sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
WCachedSample::WCachedSample(){
|
WCachedSample::WCachedSample(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached sample created.\n");
|
|
||||||
#endif
|
|
||||||
sample = NULL;
|
sample = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
WCachedSample::~WCachedSample(){
|
WCachedSample::~WCachedSample(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached sample destroyed.\n");
|
|
||||||
#endif
|
|
||||||
SAFE_DELETE(sample);
|
SAFE_DELETE(sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,9 +369,6 @@ unsigned long WCachedSample::size(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool WCachedSample::isGood(){
|
bool WCachedSample::isGood(){
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if(!sample || !sample->mSample)
|
if(!sample || !sample->mSample)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -436,18 +393,12 @@ bool WCachedSample::Attempt(string filename, int submode, int & error){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
locks = WRES_UNLOCKED;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//WCachedParticles
|
//WCachedParticles
|
||||||
|
|
||||||
bool WCachedParticles::isGood(){
|
bool WCachedParticles::isGood(){
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if(!particles)
|
if(!particles)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
@@ -493,10 +444,6 @@ bool WCachedParticles::Attempt(string filename, int submode, int & error){
|
|||||||
|
|
||||||
particles->sprite=NULL;
|
particles->sprite=NULL;
|
||||||
error = CACHE_ERROR_NONE;
|
error = CACHE_ERROR_NONE;
|
||||||
|
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
locks = WRES_UNLOCKED;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -505,15 +452,9 @@ hgeParticleSystemInfo * WCachedParticles::Actual(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
WCachedParticles::WCachedParticles(){
|
WCachedParticles::WCachedParticles(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached particles created.\n");
|
|
||||||
#endif
|
|
||||||
particles = NULL;
|
particles = NULL;
|
||||||
}
|
}
|
||||||
WCachedParticles::~WCachedParticles(){
|
WCachedParticles::~WCachedParticles(){
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("Cached particles destroyed.\n");
|
|
||||||
#endif
|
|
||||||
SAFE_DELETE(particles);
|
SAFE_DELETE(particles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -523,11 +464,6 @@ void WCachedParticles::Nullify(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WCachedParticles::Trash(){
|
void WCachedParticles::Trash(){
|
||||||
id = "";
|
|
||||||
locks = WRES_TRASH;
|
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("WCachedParticles::Trash()\n");
|
|
||||||
#endif
|
|
||||||
SAFE_DELETE(particles);
|
SAFE_DELETE(particles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -537,11 +473,6 @@ void WTrackedQuad::Nullify() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WTrackedQuad::Trash(){
|
void WTrackedQuad::Trash(){
|
||||||
id = "";
|
|
||||||
locks = WRES_TRASH;
|
|
||||||
#ifdef DEBUG_CACHE
|
|
||||||
OutputDebugString("WTrackedQuad::Trash()\n");
|
|
||||||
#endif
|
|
||||||
resname.clear();
|
resname.clear();
|
||||||
SAFE_DELETE(quad);
|
SAFE_DELETE(quad);
|
||||||
}
|
}
|
||||||
@@ -554,9 +485,6 @@ unsigned long WTrackedQuad::size() {
|
|||||||
return sizeof(JQuad);
|
return sizeof(JQuad);
|
||||||
}
|
}
|
||||||
bool WTrackedQuad::isGood(){
|
bool WTrackedQuad::isGood(){
|
||||||
if(locks == WRES_TRASH)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return (quad != NULL);
|
return (quad != NULL);
|
||||||
}
|
}
|
||||||
WTrackedQuad::WTrackedQuad(string _resname) {
|
WTrackedQuad::WTrackedQuad(string _resname) {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user