Source code of Windows XP (NT5)
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.

201 lines
3.7 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. virtual ~CACHE_HINT_ENTRY()
  68. {
  69. }
  70. LONG _cRefs;
  71. LONG _cTTL;
  72. LONG _cConfiguredTTL;
  73. DWORD _cLastUsageTime;
  74. STRU _strHintKey;
  75. WCHAR _achHintKey[ 64 ];
  76. };
  77. class CACHE_HINT_HASH : public CTypedHashTable< CACHE_HINT_HASH,
  78. CACHE_HINT_ENTRY,
  79. WCHAR * >
  80. {
  81. public:
  82. CACHE_HINT_HASH() : CTypedHashTable< CACHE_HINT_HASH,
  83. CACHE_HINT_ENTRY,
  84. WCHAR * >
  85. ( "CACHE_HINT_HASH" )
  86. {
  87. }
  88. static WCHAR *
  89. ExtractKey(
  90. const CACHE_HINT_ENTRY * pHintEntry
  91. )
  92. {
  93. return pHintEntry->QueryKey();
  94. }
  95. static DWORD
  96. CalcKeyHash(
  97. const WCHAR * pszHintKey
  98. )
  99. {
  100. return HashString( pszHintKey );
  101. }
  102. static bool
  103. EqualKeys(
  104. const WCHAR * pKey1,
  105. const WCHAR * pKey2
  106. )
  107. {
  108. return wcscmp( pKey1, pKey2 ) == 0;
  109. }
  110. static void
  111. AddRefRecord(
  112. CACHE_HINT_ENTRY * pHintEntry,
  113. int nIncr
  114. )
  115. {
  116. DBG_ASSERT( pHintEntry != NULL );
  117. if ( nIncr < 0 )
  118. {
  119. pHintEntry->Release();
  120. }
  121. else if ( nIncr > 0 )
  122. {
  123. pHintEntry->AddRef();
  124. pHintEntry->ResetTTL();
  125. }
  126. else
  127. {
  128. DBG_ASSERT( FALSE );
  129. }
  130. }
  131. };
  132. class CACHE_HINT_MANAGER
  133. {
  134. public:
  135. CACHE_HINT_MANAGER();
  136. virtual ~CACHE_HINT_MANAGER();
  137. HRESULT
  138. Initialize(
  139. CACHE_HINT_CONFIG * pConfig
  140. );
  141. HRESULT
  142. ShouldCacheEntry(
  143. CACHE_KEY * pCacheKey,
  144. BOOL * pfShouldCache
  145. );
  146. static
  147. LK_PREDICATE
  148. HintFlushByTTL(
  149. CACHE_HINT_ENTRY * pHintEntry,
  150. VOID * pvState
  151. );
  152. VOID
  153. FlushByTTL(
  154. VOID
  155. );
  156. static
  157. VOID
  158. WINAPI
  159. ScavengerCallback(
  160. PVOID pParam,
  161. BOOLEAN TimerOrWaitFired
  162. );
  163. private:
  164. CACHE_HINT_HASH _hintTable;
  165. HANDLE _hTimer;
  166. DWORD _cConfiguredTTL;
  167. DWORD _cmsecActivityWindow;
  168. };
  169. #endif