00001 /****************************************************************************** 00002 * 00003 * $Id:$ 00004 * 00005 * Copyright (C) 1997-2008 by Dimitri van Heesch. 00006 * 00007 * Permission to use, copy, modify, and distribute this software and its 00008 * documentation under the terms of the GNU General Public License is hereby 00009 * granted. No representations are made about the suitability of this software 00010 * for any purpose. It is provided "as is" without express or implied warranty. 00011 * See the GNU General Public License for more details. 00012 * 00013 * Documents produced by Doxygen are derivative works derived from the 00014 * input used in their production; they are not affected by this license. 00015 * 00016 */ 00017 00018 #ifndef OBJCACHE_H 00019 #define OBJCACHE_H 00020 00021 //#define CACHE_TEST 00022 //#define CACHE_DEBUG 00023 #define CACHE_STATS 00024 00033 class ObjCache 00034 { 00035 private: 00036 struct CacheNode 00037 { 00038 CacheNode() : next(-1), prev(-1), obj(0) {} 00039 int next; 00040 int prev; 00041 void *obj; 00042 }; 00043 struct HashNode 00044 { 00045 HashNode() : head(-1), nextHash(-1), index(-1), obj(0) {} 00046 int head; 00047 int nextHash; 00048 int index; 00049 void *obj; 00050 }; 00051 00052 public: 00056 ObjCache(unsigned int logSize); 00057 00059 ~ObjCache(); 00060 00066 int add(void *obj,void **victim); 00067 00072 void use(int handle) 00073 { 00074 hits++; 00075 if (handle==m_lastHandle) return; 00076 m_lastHandle = handle; 00077 moveToFront(handle); 00078 } 00079 00083 void del(int handle); 00084 00086 void printLRU(); 00088 void printStats(); 00089 00090 private: 00091 void moveToFront(int index); 00092 unsigned int hash(void *addr); 00093 HashNode *hashFind(void *obj); 00094 HashNode *hashInsert(void *obj); 00095 void hashRemove(void *obj); 00096 00097 CacheNode *m_cache; 00098 HashNode *m_hash; 00099 int m_head; 00100 int m_tail; 00101 int m_size; 00102 int m_freeHashNodes; 00103 int m_freeCacheNodes; 00104 int m_lastHandle; 00105 00106 #ifdef CACHE_STATS 00107 static int misses; 00108 static int hits; 00109 #endif 00110 }; 00111 00112 #endif // OBJCACHE_H 00113