Blame src/objcache.h

Packit Service 50c9f2
/******************************************************************************
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * 
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Permission to use, copy, modify, and distribute this software and its
Packit Service 50c9f2
 * documentation under the terms of the GNU General Public License is hereby 
Packit Service 50c9f2
 * granted. No representations are made about the suitability of this software 
Packit Service 50c9f2
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit Service 50c9f2
 * See the GNU General Public License for more details.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Documents produced by Doxygen are derivative works derived from the
Packit Service 50c9f2
 * input used in their production; they are not affected by this license.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 */
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef OBJCACHE_H
Packit Service 50c9f2
#define OBJCACHE_H
Packit Service 50c9f2
Packit Service 50c9f2
//#define CACHE_TEST
Packit Service 50c9f2
//#define CACHE_DEBUG
Packit Service 50c9f2
#define CACHE_STATS
Packit Service 50c9f2
Packit Service 50c9f2
/** @brief Cache for objects.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *  This cache is used to decide which objects should remain in
Packit Service 50c9f2
 *  memory. It uses a least recently used policy (LRU) to decide
Packit Service 50c9f2
 *  which object should make room for a new object when the cache
Packit Service 50c9f2
 *  is full. An object should be added using add(), and then use()
Packit Service 50c9f2
 *  should be called when the object is used.
Packit Service 50c9f2
 */
Packit Service 50c9f2
class ObjCache
Packit Service 50c9f2
{
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    struct CacheNode
Packit Service 50c9f2
    {
Packit Service 50c9f2
      CacheNode() : next(-1), prev(-1), obj(0) {}
Packit Service 50c9f2
      int next;
Packit Service 50c9f2
      int prev;
Packit Service 50c9f2
      void *obj;
Packit Service 50c9f2
    };
Packit Service 50c9f2
    struct HashNode
Packit Service 50c9f2
    {
Packit Service 50c9f2
      HashNode() : head(-1), nextHash(-1), index(-1), obj(0) {}
Packit Service 50c9f2
      int head;
Packit Service 50c9f2
      int nextHash; 
Packit Service 50c9f2
      int index;
Packit Service 50c9f2
      void *obj;
Packit Service 50c9f2
    };
Packit Service 50c9f2
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    /*! Creates the cache. The number of elements in the cache is 2 to 
Packit Service 50c9f2
     *  the power of \a logSize. 
Packit Service 50c9f2
     */
Packit Service 50c9f2
    ObjCache(unsigned int logSize);
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Deletes the cache and free all internal data-structures used. */
Packit Service 50c9f2
   ~ObjCache();
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Adds \a obj to the cache. When victim is not null, this object is
Packit Service 50c9f2
     *  removed from the cache to make room for \a obj. 
Packit Service 50c9f2
     *  Returns a handle to the object, which can be used by the use()
Packit Service 50c9f2
     *  function, each time the object is used.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    int add(void *obj,void **victim);
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Indicates that this object is used. This will move the object
Packit Service 50c9f2
     *  to the front of the internal LRU list to make sure it is removed last.
Packit Service 50c9f2
     *  The parameter \a handle is returned when called add().
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void use(int handle)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      if (handle==m_lastHandle) return;
Packit Service 50c9f2
      m_lastHandle = handle;
Packit Service 50c9f2
      m_hits++;
Packit Service 50c9f2
      moveToFront(handle);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Removes the item identified by \a handle from the cache.
Packit Service 50c9f2
     *  @see add()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void del(int handle);
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Debug function. Prints the LRU list */
Packit Service 50c9f2
    void printLRU();
Packit Service 50c9f2
    /*! Print miss/hits statistics */
Packit Service 50c9f2
    void printStats();
Packit Service 50c9f2
Packit Service 50c9f2
    /*! total size of the cache */
Packit Service 50c9f2
    int size() const { return m_size; }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! number of elements in the cache */
Packit Service 50c9f2
    int count() const { return m_count; }
Packit Service 50c9f2
Packit Service 50c9f2
    int hits() const 
Packit Service 50c9f2
    { 
Packit Service 50c9f2
      return m_hits; 
Packit Service 50c9f2
    }
Packit Service 50c9f2
    int misses() const 
Packit Service 50c9f2
    { 
Packit Service 50c9f2
      return m_misses; 
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    void moveToFront(int index);
Packit Service 50c9f2
    unsigned int hash(void *addr);
Packit Service 50c9f2
    HashNode *hashFind(void *obj);
Packit Service 50c9f2
    HashNode *hashInsert(void *obj);
Packit Service 50c9f2
    void hashRemove(void *obj);
Packit Service 50c9f2
Packit Service 50c9f2
    CacheNode *m_cache;
Packit Service 50c9f2
    HashNode  *m_hash;
Packit Service 50c9f2
    int        m_head;
Packit Service 50c9f2
    int        m_tail;
Packit Service 50c9f2
    int        m_size;
Packit Service 50c9f2
    int        m_count;
Packit Service 50c9f2
    int        m_freeHashNodes;
Packit Service 50c9f2
    int        m_freeCacheNodes;
Packit Service 50c9f2
    int        m_lastHandle;
Packit Service 50c9f2
    int        m_misses;
Packit Service 50c9f2
    int        m_hits;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
#endif // OBJCACHE_H
Packit Service 50c9f2