My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
objcache.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  *
4  *
5  * Copyright (C) 1997-2015 by Dimitri van Heesch.
6  *
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.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 
18 #ifndef OBJCACHE_H
19 #define OBJCACHE_H
20 
21 //#define CACHE_TEST
22 //#define CACHE_DEBUG
23 #define CACHE_STATS
24 
33 class ObjCache
34 {
35  private:
36  struct CacheNode
37  {
38  CacheNode() : next(-1), prev(-1), obj(0) {}
39  int next;
40  int prev;
41  void *obj;
42  };
43  struct HashNode
44  {
45  HashNode() : head(-1), nextHash(-1), index(-1), obj(0) {}
46  int head;
47  int nextHash;
48  int index;
49  void *obj;
50  };
51 
52  public:
56  ObjCache(unsigned int logSize);
57 
59  ~ObjCache();
60 
66  int add(void *obj,void **victim);
67 
72  void use(int handle)
73  {
74  if (handle==m_lastHandle) return;
75  m_lastHandle = handle;
76  m_hits++;
77  moveToFront(handle);
78  }
79 
83  void del(int handle);
84 
86  void printLRU();
88  void printStats();
89 
91  int size() const { return m_size; }
92 
94  int count() const { return m_count; }
95 
96  int hits() const
97  {
98  return m_hits;
99  }
100  int misses() const
101  {
102  return m_misses;
103  }
104 
105 
106  private:
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);
112 
115  int m_head;
116  int m_tail;
117  int m_size;
118  int m_count;
122  int m_misses;
123  int m_hits;
124 };
125 
126 #endif // OBJCACHE_H
127