Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
4.3 KiB

  1. #ifndef _CACHEHINT_HXX_
  2. #define _CACHEHINT_HXX_
  3. class CACHE_HINT_ENTRY
  4. {
  5. public:
  6. CACHE_HINT_ENTRY( LONG cConfiguredTTL,
  7. DWORD lastUsageTime )
  8. : _strHintKey( _achHintKey, sizeof( _achHintKey ) ),
  9. _cLastUsageTime( lastUsageTime ),
  10. _cConfiguredTTL( cConfiguredTTL ),
  11. _cTTL( cConfiguredTTL ),
  12. _cRefs( 1 )
  13. {
  14. }
  15. HRESULT
  16. SetKey(
  17. WCHAR * pszKey
  18. )
  19. {
  20. return _strHintKey.Copy( pszKey );
  21. }
  22. WCHAR *
  23. QueryKey(
  24. VOID
  25. ) const
  26. {
  27. return (WCHAR*) _strHintKey.QueryStr();
  28. }
  29. BOOL
  30. QueryIsOkToFlushTTL(
  31. VOID
  32. )
  33. {
  34. if ( InterlockedDecrement( &_cTTL ) == 0 )
  35. {
  36. return TRUE;
  37. }
  38. return FALSE;
  39. }
  40. BOOL
  41. QueryIsOkToCache(
  42. DWORD tickCountNow,
  43. DWORD cmsecActivityWindow
  44. );
  45. VOID
  46. ResetTTL(
  47. VOID
  48. )
  49. {
  50. InterlockedExchange( (LPLONG) &_cTTL, _cConfiguredTTL );
  51. }
  52. VOID
  53. AddRef()
  54. {
  55. InterlockedIncrement( &_cRefs );
  56. }
  57. VOID
  58. Release()
  59. {
  60. DBG_ASSERT( _cRefs > 0);
  61. if (InterlockedDecrement( &_cRefs ) == 0)
  62. {
  63. delete this;
  64. }
  65. }
  66. private:
  67. CACHE_HINT_ENTRY(
  68. const CACHE_HINT_ENTRY &
  69. );
  70. VOID operator=(
  71. const CACHE_HINT_ENTRY &
  72. );
  73. virtual ~CACHE_HINT_ENTRY()
  74. {
  75. }
  76. LONG _cRefs;
  77. LONG _cTTL;
  78. LONG _cConfiguredTTL;
  79. DWORD _cLastUsageTime;
  80. STRU _strHintKey;
  81. WCHAR _achHintKey[ 64 ];
  82. };
  83. class CACHE_HINT_HASH : public CTypedHashTable< CACHE_HINT_HASH,
  84. CACHE_HINT_ENTRY,
  85. WCHAR * >
  86. {
  87. public:
  88. CACHE_HINT_HASH() : CTypedHashTable< CACHE_HINT_HASH,
  89. CACHE_HINT_ENTRY,
  90. WCHAR * >
  91. ( "CACHE_HINT_HASH" )
  92. {
  93. }
  94. static WCHAR *
  95. ExtractKey(
  96. const CACHE_HINT_ENTRY * pHintEntry
  97. )
  98. {
  99. return pHintEntry->QueryKey();
  100. }
  101. static DWORD
  102. CalcKeyHash(
  103. const WCHAR * pszHintKey
  104. )
  105. {
  106. return HashString( pszHintKey );
  107. }
  108. static bool
  109. EqualKeys(
  110. const WCHAR * pKey1,
  111. const WCHAR * pKey2
  112. )
  113. {
  114. return wcscmp( pKey1, pKey2 ) == 0;
  115. }
  116. static void
  117. AddRefRecord(
  118. CACHE_HINT_ENTRY * pHintEntry,
  119. int nIncr
  120. )
  121. {
  122. DBG_ASSERT( pHintEntry != NULL );
  123. if ( nIncr < 0 )
  124. {
  125. pHintEntry->Release();
  126. }
  127. else if ( nIncr > 0 )
  128. {
  129. pHintEntry->AddRef();
  130. pHintEntry->ResetTTL();
  131. }
  132. else
  133. {
  134. DBG_ASSERT( FALSE );
  135. }
  136. }
  137. private:
  138. CACHE_HINT_HASH(
  139. const CACHE_HINT_HASH &
  140. );
  141. VOID operator=(
  142. const CACHE_HINT_HASH &
  143. );
  144. };
  145. class CACHE_HINT_MANAGER
  146. {
  147. public:
  148. CACHE_HINT_MANAGER();
  149. virtual ~CACHE_HINT_MANAGER();
  150. HRESULT
  151. Initialize(
  152. CACHE_HINT_CONFIG * pConfig
  153. );
  154. HRESULT
  155. ShouldCacheEntry(
  156. CACHE_KEY * pCacheKey,
  157. BOOL * pfShouldCache
  158. );
  159. static
  160. LK_PREDICATE
  161. HintFlushByTTL(
  162. CACHE_HINT_ENTRY * pHintEntry,
  163. VOID * pvState
  164. );
  165. VOID
  166. FlushByTTL(
  167. VOID
  168. );
  169. static
  170. VOID
  171. WINAPI
  172. ScavengerCallback(
  173. PVOID pParam,
  174. BOOLEAN TimerOrWaitFired
  175. );
  176. private:
  177. CACHE_HINT_HASH _hintTable;
  178. HANDLE _hTimer;
  179. DWORD _cConfiguredTTL;
  180. DWORD _cmsecActivityWindow;
  181. CACHE_HINT_MANAGER(
  182. const CACHE_HINT_MANAGER &
  183. );
  184. VOID operator=(
  185. const CACHE_HINT_MANAGER &
  186. );
  187. };
  188. #endif