Cache for objects. More...
#include <objcache.h>
Classes | |
struct | CacheNode |
struct | HashNode |
Public Member Functions | |
ObjCache (unsigned int logSize) | |
~ObjCache () | |
int | add (void *obj, void **victim) |
void | use (int handle) |
void | del (int handle) |
void | printLRU () |
void | printStats () |
int | size () const |
int | count () const |
int | hits () const |
int | misses () const |
Private Member Functions | |
void | moveToFront (int index) |
unsigned int | hash (void *addr) |
HashNode * | hashFind (void *obj) |
HashNode * | hashInsert (void *obj) |
void | hashRemove (void *obj) |
Private Attributes | |
CacheNode * | m_cache |
HashNode * | m_hash |
int | m_head |
int | m_tail |
int | m_size |
int | m_count |
int | m_freeHashNodes |
int | m_freeCacheNodes |
int | m_lastHandle |
int | m_misses |
int | m_hits |
Cache for objects.
This cache is used to decide which objects should remain in memory. It uses a least recently used policy (LRU) to decide which object should make room for a new object when the cache is full. An object should be added using add(), and then use() should be called when the object is used.
Definition at line 33 of file objcache.h.
ObjCache::ObjCache | ( | unsigned int | logSize | ) |
Creates the cache. The number of elements in the cache is 2 to the power of logSize.
Definition at line 28 of file objcache.cpp.
ObjCache::~ObjCache | ( | ) |
Deletes the cache and free all internal data-structures used.
Definition at line 46 of file objcache.cpp.
int ObjCache::add | ( | void * | obj, |
void ** | victim | ||
) |
Adds obj to the cache. When victim is not null, this object is removed from the cache to make room for obj. Returns a handle to the object, which can be used by the use() function, each time the object is used.
Definition at line 52 of file objcache.cpp.
|
inline |
void ObjCache::del | ( | int | handle | ) |
Removes the item identified by handle from the cache.
Definition at line 105 of file objcache.cpp.
|
private |
Definition at line 175 of file objcache.cpp.
|
private |
Definition at line 206 of file objcache.cpp.
|
private |
Definition at line 225 of file objcache.cpp.
|
private |
Definition at line 255 of file objcache.cpp.
|
inline |
Definition at line 96 of file objcache.h.
|
inline |
Definition at line 100 of file objcache.h.
|
private |
Definition at line 155 of file objcache.cpp.
void ObjCache::printLRU | ( | ) |
Debug function. Prints the LRU list
void ObjCache::printStats | ( | ) |
Print miss/hits statistics
Definition at line 149 of file objcache.cpp.
|
inline |
|
inline |
Indicates that this object is used. This will move the object to the front of the internal LRU list to make sure it is removed last. The parameter handle is returned when called add().
Definition at line 72 of file objcache.h.
|
private |
Definition at line 113 of file objcache.h.
|
private |
Definition at line 118 of file objcache.h.
|
private |
Definition at line 120 of file objcache.h.
|
private |
Definition at line 119 of file objcache.h.
|
private |
Definition at line 114 of file objcache.h.
|
private |
Definition at line 115 of file objcache.h.
|
private |
Definition at line 123 of file objcache.h.
|
private |
Definition at line 121 of file objcache.h.
|
private |
Definition at line 122 of file objcache.h.
|
private |
Definition at line 117 of file objcache.h.
|
private |
Definition at line 116 of file objcache.h.