ObjectCache Class Reference

Base class for the cache. The cache replacement policy algorithms are implemented in the derived classes such as ObjectCacheRand...etc. More...

#include <objcache.h>

Inheritance diagram for ObjectCache:

CCObject SimpleCCObject ObjectCacheFIFO ObjectCacheRandom ObjectCacheWeakest List of all members.

Public Member Functions

 ObjectCache ()
 default constructor for ObjectCache Note:
 ObjectCache (UINT32 ceiling)
 constructor for ObjectCache, set the cache size.
 ~ObjectCache ()
 destructor for ObjectCache Note:
virtual WORD Add (CachedObject *pObj)
 Add an object in the Hash table of the cache Note:.
virtual CachedObjectLookUp (WORD hObj)
 LookUp in the hash table.
virtual BOOL Remove (WORD hObj)
 Remove a key from the hash table Note:.
virtual BOOL SetMaxCacheSize (UINT32 NewCeiling)
 dynamically change the size (in bytes) of the Cache. Note:

Protected Member Functions

virtual BOOL MakeRoomFor (CachedObject *pObj)
 make room for another object by deleting old objects if the cache is full Note:
virtual BOOL DeleteObject ()
 virtual function. This is the base class delete function. All algorithms are implemented in the derived classes such as ObjectCacheRandom...etc. Note:

Protected Attributes

UINT32 m_Ceiling
UINT32 m_CurrentSize
WORD m_NumObjects
std::map< WORD, PVOIDm_ObjectMap
WORD m_HandleFactory

Private Member Functions

 CC_DECLARE_DYNCREATE (ObjectCache)

Detailed Description

Base class for the cache. The cache replacement policy algorithms are implemented in the derived classes such as ObjectCacheRand...etc.

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
20/01/97

Definition at line 121 of file objcache.h.


Constructor & Destructor Documentation

ObjectCache::ObjectCache  ) 
 

default constructor for ObjectCache Note:

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
20/01/97
See also:

Definition at line 130 of file objcache.cpp.

00131 {
00132     // Compute the available amount of memory
00133     /*TunedMemory* pTuneMem = GetTunedMemManager();
00134     if (pTuneMem != NULL)
00135         Ceiling = pTuneMem->GetAvailableTunedMem();*/
00136 
00137     m_NumObjects    = 0;
00138     m_HandleFactory = 1;
00139     m_CurrentSize   = 0;
00140 }

ObjectCache::ObjectCache UINT32  ceiling  ) 
 

constructor for ObjectCache, set the cache size.

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
20/01/97
Parameters:
ceiling,: the amount of memory available for the cache [INPUTS] Note:
See also:

Definition at line 153 of file objcache.cpp.

00154 {
00155     m_Ceiling       = ceiling;
00156     m_NumObjects    = 0;
00157     m_HandleFactory = 1;    // we don't start from 0 
00158     m_CurrentSize   = 0;
00159 }

ObjectCache::~ObjectCache  ) 
 

destructor for ObjectCache Note:

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
20/01/97
See also:

Definition at line 171 of file objcache.cpp.

00172 {}


Member Function Documentation

WORD ObjectCache::Add CachedObject pObj  )  [virtual]
 

Add an object in the Hash table of the cache Note:.

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/01/97
Parameters:
pObj,: the Object to cache [INPUTS]
See also:

Definition at line 212 of file objcache.cpp.

00213 {
00214     ERROR3IF(pObj==NULL, "Error: the object to cache can't be NULL");
00215 
00216     if (pObj == NULL)
00217         return 0;
00218 
00219 //  MYTRACE( _T("ObjectCache::Add()\n"));
00220 
00221     // if we can make room for the object then assign the object a unique handle and cache it
00222     if (MakeRoomFor(pObj))
00223     {
00224         CACHE_HANDLE hObj = m_HandleFactory++;
00225 
00226         pObj->StoreHandle(hObj);        // store the CachedObject Handle
00227         m_NumObjects++;                 // Increase the number of objects
00228         m_CurrentSize += pObj->GetSize();   // Increase the space used
00229         m_ObjectMap[hObj] = pObj;   // set the object in the hash table
00230         return hObj;
00231     }
00232 
00233     return 0;
00234 }

ObjectCache::CC_DECLARE_DYNCREATE ObjectCache   )  [private]
 

BOOL ObjectCache::DeleteObject  )  [protected, virtual]
 

virtual function. This is the base class delete function. All algorithms are implemented in the derived classes such as ObjectCacheRandom...etc. Note:

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/01/97
See also:

Reimplemented in ObjectCacheFIFO, ObjectCacheRandom, and ObjectCacheWeakest.

Definition at line 340 of file objcache.cpp.

00341 {
00342     return TRUE;
00343 }

CachedObject * ObjectCache::LookUp WORD  hObj  )  [virtual]
 

LookUp in the hash table.

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/01/97
Returns:
return a pointer to the object found, NULL otherwise Note:
See also:

Definition at line 294 of file objcache.cpp.

00295 {
00296 //  void* pObjMap = NULL;
00297 
00298     std::map<WORD, PVOID>::iterator iter = m_ObjectMap.find( hObj );
00299     if( m_ObjectMap.end() != iter  )
00300         return (CachedObject *)iter->second;
00301 
00302     return NULL;
00303 }

BOOL ObjectCache::MakeRoomFor CachedObject pObj  )  [protected, virtual]
 

make room for another object by deleting old objects if the cache is full Note:

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/01/97
Parameters:
pObj,: The object to add in the cache [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise
See also:

Definition at line 250 of file objcache.cpp.

00251 {
00252     ERROR3IF(pObj==NULL, "Error: the object to cache can't be NULL");
00253 
00254     if (pObj == NULL)
00255         return FALSE;
00256 
00257     // is there room in the cache or do we have to free some memory ?
00258     if (pObj->GetSize() + m_CurrentSize <= m_Ceiling)
00259         return TRUE;    // enough room
00260 
00261     // is the cache big enough ? 
00262     if (pObj->GetSize() > m_Ceiling)
00263         return FALSE;   // the object is bigger than the cache size
00264 
00265     // Scan the cache deleting objects until there is room
00266     BOOL EnoughRoom = (pObj->GetSize() + m_CurrentSize <= m_Ceiling);
00267     do
00268     {
00269         BOOL ObjectDeleted = DeleteObject();
00270 
00271         EnoughRoom = (pObj->GetSize() + m_CurrentSize <= m_Ceiling);
00272 
00273         if(!ObjectDeleted && !EnoughRoom)
00274             return FALSE;       // no room and no deleted objects 
00275     }
00276     while (!EnoughRoom);
00277 
00278     return TRUE;
00279 }

BOOL ObjectCache::Remove WORD  hObj  )  [virtual]
 

Remove a key from the hash table Note:.

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/01/97
Parameters:
hObj,: the object handle [INPUTS]
Returns:
TRUE if success, FALSE otherwise
See also:

Definition at line 319 of file objcache.cpp.

00320 {
00321     if( m_ObjectMap.erase( hObj ) )
00322         return TRUE;
00323 
00324     return FALSE;
00325 }

BOOL ObjectCache::SetMaxCacheSize UINT32  NewCeiling  )  [virtual]
 

dynamically change the size (in bytes) of the Cache. Note:

Author:
Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com>
Date:
20/01/97
Parameters:
NewCeiling,: The size of the new ceiling [INPUTS]
See also:

Definition at line 187 of file objcache.cpp.

00188 {
00189 //  MYTRACE( _T("ObjectCache::SetMaxCacheSize()\n"));
00190     while (m_CurrentSize > NewCeiling)
00191     {
00192         if(!DeleteObject())
00193             break;
00194     }
00195     m_Ceiling = NewCeiling;
00196     return TRUE;
00197 }


Member Data Documentation

UINT32 ObjectCache::m_Ceiling [protected]
 

Definition at line 137 of file objcache.h.

UINT32 ObjectCache::m_CurrentSize [protected]
 

Definition at line 138 of file objcache.h.

WORD ObjectCache::m_HandleFactory [protected]
 

Definition at line 141 of file objcache.h.

WORD ObjectCache::m_NumObjects [protected]
 

Definition at line 139 of file objcache.h.

std::map<WORD, PVOID> ObjectCache::m_ObjectMap [protected]
 

Definition at line 140 of file objcache.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:57:16 2007 for Camelot by  doxygen 1.4.4