Switch to using -I to find includes, rather than relative paths.
[gemrb.git] / gemrb / plugins / Core / Cache.h
blobb3f7fffe3fa7e1473ade2c7d65a115bd253438af
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 |Avenger|
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef CACHE_H
22 #define CACHE_H
24 #include <ctype.h>
25 #include <cstring>
26 #include "win32def.h"
27 #include "globals.h"
28 /////////////////////////////////////////////////////////////////////////////
29 // Cache<ieResRef, void*>
31 #define KEYSIZE 8
33 #ifndef ReleaseFun
34 typedef void (*ReleaseFun)(void *);
35 #endif
37 class Cache
39 protected:
40 // Association
41 struct MyAssoc {
42 MyAssoc* pNext;
43 MyAssoc** pPrev;
44 char key[KEYSIZE]; //not ieResRef!
45 ieDword nRefCount;
46 void* data;
48 struct MemBlock {
49 MemBlock* pNext;
52 public:
53 // Construction
54 Cache(int nBlockSize = 10, int nHashTableSize = 129);
56 // Attributes
57 // number of elements
58 inline int GetCount() const
60 return m_nCount;
62 inline bool IsEmpty() const
64 return m_nCount==0;
66 // Lookup
67 void *GetResource(const ieResRef key);
68 // Operations
69 bool SetAt(const ieResRef key, void *rValue);
70 // decreases refcount or drops data
71 //if name is supplied it is faster, it will use rValue to validate the request
72 int DecRef(void *rValue, const ieResRef name, bool free);
73 int RefCount(const ieResRef key) const;
74 void RemoveAll(ReleaseFun fun);//removes all refcounts
75 void Cleanup(); //removes only zero refcounts
76 void InitHashTable(unsigned int hashSize, bool bAllocNow = true);
78 // Implementation
79 protected:
80 MyAssoc** m_pHashTable;
81 unsigned int m_nHashTableSize;
82 int m_nCount;
83 MyAssoc* m_pFreeList;
84 MemBlock* m_pBlocks;
85 int m_nBlockSize;
87 Cache::MyAssoc* NewAssoc();
88 void FreeAssoc(Cache::MyAssoc*);
89 Cache::MyAssoc* GetAssocAt(const ieResRef) const;
90 Cache::MyAssoc *GetNextAssoc(Cache::MyAssoc * rNextPosition) const;
91 unsigned int MyHashKey(const ieResRef) const;
93 public:
94 ~Cache();
97 #endif //CACHE_H