convert line ends
[canaan.git] / prj / cam / src / framewrk / lr_cache.cpp
blob02a7e481cb50316b668035708766e7b42984c18f
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/src/framewrk/lr_cache.cpp,v 1.2 2000/02/19 13:16:24 toml Exp $
7 // interface to the system cache for LR
9 // when an LR entry is unlocked, LR tells this cache it is free
10 // if the cache needs it, it tells LR, who really gets rid of it
11 // if LR needs it before the cache hoses it, then LR takes it back
13 // NOTE: currently only unlocked data is in the cache!
15 #include <lg.h>
16 #include <mprintf.h>
17 #include <comtools.h>
18 #include <appagg.h>
19 #include <cacheapi.h>
21 #include <lr_cache.h>
23 #include <initguid.h>
24 #include <memall.h>
25 #include <dbmem.h> // must be last header!
26 DEFINE_LG_GUID(GUID_lrCache,0x4f);
28 // global cache data
29 static ICache * g_pLRCache = NULL;
30 static int g_HndInUse;
31 static lr_callback g_pKillCallback = NULL;
33 /////////
34 #ifdef DBG_ON
35 #define lrCache_VerifyState() if (g_pLRCache==NULL) return FALSE
36 #else
37 #define lrCache_VerifyState()
38 #endif
40 //#define LR_TALK
42 #ifdef LR_TALK
43 #define lrCacheTalk(x) mprintf x
44 #define lrCacheRun(x) x
45 static int kill_count;
46 #else
47 #define lrCacheTalk(x)
48 #define lrCacheRun(x)
49 #endif
51 ////////////
52 // cache control callback
54 // return S_FALSE to mean dont do the delete....
55 HRESULT LGAPI _lrCache_CallbackFunc(const sCacheMsg * pMsg)
57 BOOL do_delete=TRUE;
58 lrCacheTalk(("KillCall %x (pos %d) (item %x) (%x)..",pMsg->message,pMsg->itemId,pMsg->pItem,kCM_DeleteOnAge));
59 lrCacheRun(kill_count++);
60 switch (pMsg->message)
62 case kCM_DeleteOnAge:
63 break;
64 case kCM_DeleteOnFlush:
65 if ((int)pMsg->itemId == g_HndInUse)
66 do_delete=FALSE;
67 break;
69 if (do_delete&&g_pKillCallback)
70 do_delete=(*g_pKillCallback)((int)pMsg->itemId, pMsg->pItem);
71 return do_delete?S_OK:S_FALSE;
74 ///////////
75 // init/term of cache
77 // pass in a size?
78 void lrCache_Init(lr_callback toast_it, int size)
80 AutoAppIPtr(SharedCache);
82 if (pSharedCache)
84 sCacheClientDesc cacheClientDesc;
86 cacheClientDesc.pID = &GUID_lrCache;
87 cacheClientDesc.pContext = NULL;
88 cacheClientDesc.pfnCallback = _lrCache_CallbackFunc;
89 cacheClientDesc.nMaxBytes = (ulong) size;
90 cacheClientDesc.nMaxItems = (ulong) -1;
91 cacheClientDesc.flags = 0;
93 pSharedCache->AddClient(&cacheClientDesc, &g_pLRCache);
95 g_pKillCallback = toast_it;
97 else
98 CriticalMsg("Expected shared cache");
101 void lrCache_Term(void)
103 if (g_pLRCache)
105 g_pLRCache->FlushAll();
106 SafeRelease(g_pLRCache);
107 g_pLRCache=NULL;
111 ////////////
112 // lock/free behavior
114 // there are two mutator calls to the cache
115 // StoreFreedData and RetrieveFreedData
116 // as well as the callback installed in Init on Cache delete
118 BOOL lrCache_StoreFreedData(int hnd, void *pData, int size)
120 lrCache_VerifyState();
121 g_HndInUse=hnd;
122 lrCacheTalk(("Storing %d at %x size %x\n",hnd,pData,size));
123 VerifyMsg1(g_pLRCache->Add((tCacheItemID)hnd, pData, size) == S_OK,
124 "hnd %i already in cache!\n", hnd );
125 g_HndInUse=0;
126 return TRUE;
129 BOOL lrCache_TakeBackData(int hnd)
131 void *pData; // returns TRUE if S_OK, ie. if we were in the cache
132 BOOL rv;
133 lrCache_VerifyState();
134 rv=(g_pLRCache->Remove((tCacheItemID)hnd, &pData) == S_OK);
135 lrCacheTalk(("Took back %d (rv %d) (data %x)\n",hnd,rv,pData));
136 return rv;
139 ////////////
140 // flush control
142 BOOL lrCache_FlushAll(void)
144 lrCache_VerifyState();
145 #ifdef LR_TALK
147 sCacheState state;
148 lrCacheRun(g_pLRCache->GetState(&state));
149 lrCacheRun(kill_count=0);
150 lrCacheTalk(("nItems %d..",state.nItems));
152 #endif
153 g_pLRCache->FlushAll();
154 lrCacheTalk(("kill count %d..",kill_count));
155 return TRUE;
158 BOOL lrCache_FlushHandle(int hnd)
160 lrCache_VerifyState();
161 if (g_HndInUse != hnd)
162 g_pLRCache->Flush(hnd);
163 return TRUE;
166 /////////////
167 // utilities to mainpulate shared cache
169 // idiotic!!! i should just return a struct! kill me now!!
171 int lrCache_GetSize(int *maxBytes)
173 sCacheState state;
174 lrCache_VerifyState();
175 g_pLRCache->GetState(&state);
176 if (maxBytes) *maxBytes=state.nMaxBytes;
177 return state.nBytes;
180 int lrCache_GetGlobalSize(int *maxBytes)
182 sCacheState state;
183 AutoAppIPtr(SharedCache);
184 pSharedCache->GetState(&state);
185 if (maxBytes) *maxBytes=state.nMaxBytes;
186 return state.nBytes;