2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
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!
25 #include <dbmem.h> // must be last header!
26 DEFINE_LG_GUID(GUID_lrCache
,0x4f);
29 static ICache
* g_pLRCache
= NULL
;
30 static int g_HndInUse
;
31 static lr_callback g_pKillCallback
= NULL
;
35 #define lrCache_VerifyState() if (g_pLRCache==NULL) return FALSE
37 #define lrCache_VerifyState()
43 #define lrCacheTalk(x) mprintf x
44 #define lrCacheRun(x) x
45 static int kill_count
;
47 #define lrCacheTalk(x)
52 // cache control callback
54 // return S_FALSE to mean dont do the delete....
55 HRESULT LGAPI
_lrCache_CallbackFunc(const sCacheMsg
* pMsg
)
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
)
64 case kCM_DeleteOnFlush
:
65 if ((int)pMsg
->itemId
== g_HndInUse
)
69 if (do_delete
&&g_pKillCallback
)
70 do_delete
=(*g_pKillCallback
)((int)pMsg
->itemId
, pMsg
->pItem
);
71 return do_delete
?S_OK
:S_FALSE
;
78 void lrCache_Init(lr_callback toast_it
, int size
)
80 AutoAppIPtr(SharedCache
);
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
;
98 CriticalMsg("Expected shared cache");
101 void lrCache_Term(void)
105 g_pLRCache
->FlushAll();
106 SafeRelease(g_pLRCache
);
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();
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
);
129 BOOL
lrCache_TakeBackData(int hnd
)
131 void *pData
; // returns TRUE if S_OK, ie. if we were in the cache
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
));
142 BOOL
lrCache_FlushAll(void)
144 lrCache_VerifyState();
148 lrCacheRun(g_pLRCache
->GetState(&state
));
149 lrCacheRun(kill_count
=0);
150 lrCacheTalk(("nItems %d..",state
.nItems
));
153 g_pLRCache
->FlushAll();
154 lrCacheTalk(("kill count %d..",kill_count
));
158 BOOL
lrCache_FlushHandle(int hnd
)
160 lrCache_VerifyState();
161 if (g_HndInUse
!= hnd
)
162 g_pLRCache
->Flush(hnd
);
167 // utilities to mainpulate shared cache
169 // idiotic!!! i should just return a struct! kill me now!!
171 int lrCache_GetSize(int *maxBytes
)
174 lrCache_VerifyState();
175 g_pLRCache
->GetState(&state
);
176 if (maxBytes
) *maxBytes
=state
.nMaxBytes
;
180 int lrCache_GetGlobalSize(int *maxBytes
)
183 AutoAppIPtr(SharedCache
);
184 pSharedCache
->GetState(&state
);
185 if (maxBytes
) *maxBytes
=state
.nMaxBytes
;