1 /******************************************************************************
3 * $Id: classlist.cpp,v 1.14 2001/03/19 19:27:39 root Exp $
5 * Copyright (C) 1997-2012 by Dimitri van Heesch.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation under the terms of the GNU General Public License is hereby
9 * granted. No representations are made about the suitability of this software
10 * for any purpose. It is provided "as is" without express or implied warranty.
11 * See the GNU General Public License for more details.
13 * Documents produced by Doxygen are derivative works derived from the
14 * input used in their production; they are not affected by this license.
23 //----------------------------------------------------------------------
25 ObjCache::ObjCache(unsigned int logSize)
26 : m_head(-1), m_tail(-1), //m_numEntries(0),
27 m_size(1<<logSize), m_count(0), m_freeHashNodes(0), m_freeCacheNodes(0),
31 m_cache = new CacheNode[m_size];
32 m_hash = new HashNode[m_size];
33 // add all items to list of free buckets
34 for (i=0;i<m_size-1;i++)
36 m_hash[i].nextHash = i+1;
37 m_cache[i].next = i+1;
49 int ObjCache::add(void *obj,void **victim)
53 HashNode *hnode = hashFind(obj);
54 //printf("hnode=%p\n",hnode);
55 if (hnode) // move object to the front of the LRU list, since it is used
58 //printf("moveToFront=%d\n",hnode->index);
59 moveToFront(hnode->index);
62 else // object not in the cache.
65 if (m_freeCacheNodes!=-1) // cache not full -> add element to the cache
67 // remove element from free list
68 int index = m_freeCacheNodes;
69 m_freeCacheNodes = m_cache[index].next;
71 // add to head of the list
76 m_cache[index].prev = -1;
77 m_cache[index].next = m_head;
80 m_cache[m_head].prev = index;
85 else // cache full -> replace element in the cache
87 //printf("Cache full!\n");
88 lruObj = m_cache[m_tail].obj;
90 moveToFront(m_tail); // m_tail indexes the emptied element, which becomes m_head
92 //printf("numEntries=%d size=%d\n",m_numEntries,m_size);
93 m_cache[m_head].obj = obj;
94 hnode = hashInsert(obj);
95 hnode->index = m_head;
102 void ObjCache::del(int index)
105 assert(m_cache[index].obj!=0);
106 hashRemove(m_cache[index].obj);
108 m_head = m_cache[index].next;
112 m_cache[m_head].prev=-1;
113 m_cache[index].obj=0;
114 m_cache[index].prev=-1;
115 m_cache[index].next = m_freeCacheNodes;
116 m_freeCacheNodes = index;
121 #define cache_debug_printf printf
122 void ObjCache::printLRU()
124 cache_debug_printf("MRU->LRU: ");
128 cache_debug_printf("%d=%p ",index,m_cache[index].obj);
129 index = m_cache[index].next;
131 cache_debug_printf("\n");
133 cache_debug_printf("LRU->MRU: ");
137 cache_debug_printf("%d=%p ",index,m_cache[index].obj);
138 index = m_cache[index].prev;
140 cache_debug_printf("\n");
145 #define cache_stats_printf printf
146 void ObjCache::printStats()
148 cache_stats_printf("ObjCache: hits=%d misses=%d hit ratio=%f\n",m_hits,m_misses,m_hits*100.0/(m_hits+m_misses));
152 void ObjCache::moveToFront(int index)
157 next = m_cache[index].next;
158 prev = m_cache[index].prev;
160 // de-chain node at index
161 m_cache[prev].next = next;
162 if (next!=-1) m_cache[next].prev = prev; else m_tail = prev;
165 m_cache[index].prev = -1;
166 m_cache[index].next = m_head;
167 m_cache[m_head].prev = index;
172 unsigned int ObjCache::hash(void *addr)
174 static bool isPtr64 = sizeof(addr)==8;
177 uint64 key = (uint64)addr;
178 // Thomas Wang's 64 bit Mix Function
187 return key & (m_size-1);
191 // Thomas Wang's 32 bit Mix Function
192 unsigned long key = (unsigned long)addr;
199 return key & (m_size-1);
203 ObjCache::HashNode *ObjCache::hashFind(void *obj)
206 int index = m_hash[hash(obj)].head;
207 //printf("hashFind: obj=%p index=%d\n",obj,index);
209 m_hash[index].obj!=obj
210 ) // search for right object in the list
212 index = m_hash[index].nextHash;
214 // found the obj at index, so it is in the cache!
217 node = &m_hash[index];
222 ObjCache::HashNode *ObjCache::hashInsert(void *obj)
224 int index = hash(obj);
225 //printf("Inserting %p index=%d\n",obj,index);
227 // remove element from empty list
228 int newElement = m_freeHashNodes;
229 assert(newElement!=-1);
230 m_freeHashNodes = m_hash[m_freeHashNodes].nextHash;
232 if (m_hash[index].head!=-1) // hash collision -> goto end of the list
234 index = m_hash[index].head;
235 while (m_hash[index].nextHash!=-1)
237 index = m_hash[index].nextHash;
239 // add to end of the list
240 m_hash[index].nextHash = newElement;
242 else // first element in the hash list
244 m_hash[index].head = newElement;
246 // add to the end of the list
247 m_hash[newElement].nextHash = -1;
248 m_hash[newElement].obj = obj;
249 return &m_hash[newElement];
252 void ObjCache::hashRemove(void *obj)
254 int index = hash(obj);
257 int curIndex = m_hash[index].head;
259 while (m_hash[curIndex].obj!=obj)
261 prevIndex = curIndex;
262 curIndex = m_hash[curIndex].nextHash;
265 if (prevIndex==-1) // remove from start
267 m_hash[index].head = m_hash[curIndex].nextHash;
269 else // remove in the middle
271 m_hash[prevIndex].nextHash = m_hash[curIndex].nextHash;
274 // add curIndex element to empty list
275 m_hash[curIndex].nextHash = m_freeHashNodes;
276 m_hash[curIndex].index = -1;
277 m_hash[curIndex].obj = 0;
278 m_freeHashNodes = curIndex;
287 obj() : handle(-1) {}
290 obj *objs = new obj[100];
294 int objId=(i%3)+(i>>2)*4;
295 printf("------- use(%d=%p)--------\n",objId,&objs[objId]);
300 if (objs[objId].handle==-1)
302 objs[objId].handle = c.add(&objs[objId],(void**)&victim);
303 if (victim) victim->handle=-1;
307 c.use(objs[objId].handle);
309 printf("i=%d objId=%d using %p victim=%p\n",i,objId,&objs[objId],victim);
313 if (objs[i].handle!=-1)
315 printf("------ del objId=%d handle=%d ------\n",i,objs[i].handle);
316 c.del(objs[i].handle);