fixed the last commit to set and check the target more often, thanks fuzzie
[gemrb.git] / gemrb / core / Cache.h
blobb83e57641acf6cfab46204558a7dd7b9775d9799
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 "globals.h"
25 #include "win32def.h"
27 #define KEYSIZE 8
29 #ifndef ReleaseFun
30 typedef void (*ReleaseFun)(void *);
31 #endif
33 class Cache
35 protected:
36 // Association
37 struct MyAssoc {
38 MyAssoc* pNext;
39 MyAssoc** pPrev;
40 char key[KEYSIZE]; //not ieResRef!
41 ieDword nRefCount;
42 void* data;
44 struct MemBlock {
45 MemBlock* pNext;
48 public:
49 // Construction
50 Cache(int nBlockSize = 10, int nHashTableSize = 129);
52 // Attributes
53 // number of elements
54 inline int GetCount() const
56 return m_nCount;
58 inline bool IsEmpty() const
60 return m_nCount==0;
62 // Lookup
63 void *GetResource(const ieResRef key) const;
64 // Operations
65 bool SetAt(const ieResRef key, void *rValue);
66 // decreases refcount or drops data
67 //if name is supplied it is faster, it will use rValue to validate the request
68 int DecRef(void *rValue, const ieResRef name, bool free);
69 int RefCount(const ieResRef key) const;
70 void RemoveAll(ReleaseFun fun);//removes all refcounts
71 void Cleanup(); //removes only zero refcounts
72 void InitHashTable(unsigned int hashSize, bool bAllocNow = true);
74 // Implementation
75 protected:
76 MyAssoc** m_pHashTable;
77 unsigned int m_nHashTableSize;
78 int m_nCount;
79 MyAssoc* m_pFreeList;
80 MemBlock* m_pBlocks;
81 int m_nBlockSize;
83 Cache::MyAssoc* NewAssoc();
84 void FreeAssoc(Cache::MyAssoc*);
85 Cache::MyAssoc* GetAssocAt(const ieResRef) const;
86 Cache::MyAssoc *GetNextAssoc(Cache::MyAssoc * rNextPosition) const;
87 unsigned int MyHashKey(const ieResRef) const;
89 public:
90 ~Cache();
93 #endif //CACHE_H