1 /******************************************************************************
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.
25 /** @brief Cache for objects.
27 * This cache is used to decide which objects should remain in
28 * memory. It uses a least recently used policy (LRU) to decide
29 * which object should make room for a new object when the cache
30 * is full. An object should be added using add(), and then use()
31 * should be called when the object is used.
38 CacheNode() : next(-1), prev(-1), obj(0) {}
45 HashNode() : head(-1), nextHash(-1), index(-1), obj(0) {}
53 /*! Creates the cache. The number of elements in the cache is 2 to
54 * the power of \a logSize.
56 ObjCache(unsigned int logSize);
58 /*! Deletes the cache and free all internal data-structures used. */
61 /*! Adds \a obj to the cache. When victim is not null, this object is
62 * removed from the cache to make room for \a obj.
63 * Returns a handle to the object, which can be used by the use()
64 * function, each time the object is used.
66 int add(void *obj,void **victim);
68 /*! Indicates that this object is used. This will move the object
69 * to the front of the internal LRU list to make sure it is removed last.
70 * The parameter \a handle is returned when called add().
74 if (handle==m_lastHandle) return;
75 m_lastHandle = handle;
80 /*! Removes the item identified by \a handle from the cache.
85 /*! Debug function. Prints the LRU list */
87 /*! Print miss/hits statistics */
90 /*! total size of the cache */
91 int size() const { return m_size; }
93 /*! number of elements in the cache */
94 int count() const { return m_count; }
107 void moveToFront(int index);
108 unsigned int hash(void *addr);
109 HashNode *hashFind(void *obj);
110 HashNode *hashInsert(void *obj);
111 void hashRemove(void *obj);
120 int m_freeCacheNodes;