My Project
|
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.
References m_cache, m_hash, m_hits, m_misses, m_size, ObjCache::CacheNode::next, and ObjCache::HashNode::nextHash.
ObjCache::~ObjCache | ( | ) |
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.
References hashFind(), hashInsert(), hashRemove(), ObjCache::HashNode::index, m_cache, m_count, m_freeCacheNodes, m_head, m_hits, m_misses, m_tail, moveToFront(), ObjCache::CacheNode::next, ObjCache::CacheNode::obj, and ObjCache::CacheNode::prev.
|
inline |
number of elements in the cache
Definition at line 94 of file objcache.h.
References m_count.
void ObjCache::del | ( | int | handle | ) |
Removes the item identified by handle from the cache.
Definition at line 105 of file objcache.cpp.
References hashRemove(), m_cache, m_count, m_freeCacheNodes, m_head, m_tail, moveToFront(), ObjCache::CacheNode::next, ObjCache::CacheNode::obj, and ObjCache::CacheNode::prev.
|
private |
Definition at line 175 of file objcache.cpp.
References m_size.
Referenced by hashFind(), hashInsert(), and hashRemove().
|
private |
Definition at line 206 of file objcache.cpp.
References hash(), ObjCache::HashNode::head, m_hash, and ObjCache::HashNode::nextHash.
Referenced by add().
|
private |
Definition at line 225 of file objcache.cpp.
References hash(), ObjCache::HashNode::head, m_freeHashNodes, m_hash, ObjCache::HashNode::nextHash, and ObjCache::HashNode::obj.
Referenced by add().
|
private |
Definition at line 255 of file objcache.cpp.
References hash(), ObjCache::HashNode::head, ObjCache::HashNode::index, m_freeHashNodes, m_hash, ObjCache::HashNode::nextHash, and ObjCache::HashNode::obj.
Referenced by add(), and del().
|
inline |
|
inline |
|
private |
Definition at line 155 of file objcache.cpp.
References m_cache, m_head, m_tail, ObjCache::CacheNode::next, and ObjCache::CacheNode::prev.
void ObjCache::printLRU | ( | ) |
Debug function. Prints the LRU list
void ObjCache::printStats | ( | ) |
Print miss/hits statistics
Definition at line 149 of file objcache.cpp.
References cache_stats_printf, m_hits, and m_misses.
|
inline |
total size of the cache
Definition at line 91 of file objcache.h.
References m_size.
|
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.
References m_hits, m_lastHandle, and moveToFront().
|
private |
Definition at line 113 of file objcache.h.
Referenced by add(), del(), moveToFront(), ObjCache(), and ~ObjCache().
|
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.
Referenced by hashInsert(), and hashRemove().
|
private |
Definition at line 114 of file objcache.h.
Referenced by hashFind(), hashInsert(), hashRemove(), ObjCache(), and ~ObjCache().
|
private |
Definition at line 115 of file objcache.h.
Referenced by add(), del(), and moveToFront().
|
private |
Definition at line 123 of file objcache.h.
Referenced by add(), hits(), ObjCache(), printStats(), and use().
|
private |
Definition at line 121 of file objcache.h.
Referenced by use().
|
private |
Definition at line 122 of file objcache.h.
Referenced by add(), misses(), ObjCache(), and printStats().
|
private |
Definition at line 117 of file objcache.h.
Referenced by hash(), ObjCache(), and size().
|
private |
Definition at line 116 of file objcache.h.
Referenced by add(), del(), and moveToFront().