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.

214 lines
6.4 KiB

  1. /*++
  2. Copyright (C) 2000-2001 Microsoft Corporation
  3. --*/
  4. #ifndef __WMI_A51__HIECACHE__H_
  5. #define __WMI_A51__HIECACHE__H_
  6. //
  7. // NOTE: it is critical that things be marked in the cache while the repository
  8. // lock is held! Otherwise, invalidation/completion logic will break
  9. //
  10. #include <wbemint.h>
  11. #include <map>
  12. #include <sync.h>
  13. #include "a51tools.h"
  14. class wcscless : public binary_function<LPCWSTR, LPCWSTR, bool>
  15. {
  16. public:
  17. bool operator()(const LPCWSTR& wcs1, const LPCWSTR& wcs2) const
  18. {return wcscmp(wcs1, wcs2) < 0;}
  19. };
  20. typedef enum {e_KeynessUnknown, e_Keyed, e_NotKeyed} EKeyNess;
  21. class CClassRecord
  22. {
  23. protected:
  24. static long s_nRecords;
  25. long m_lRef;
  26. CClassRecord* m_pMoreRecentlyUsed;
  27. CClassRecord* m_pLessRecentlyUsed;
  28. LPWSTR m_wszClassName;
  29. WCHAR m_wszHash[MAX_HASH_LEN+1];
  30. _IWmiObject* m_pClassDef;
  31. DWORD m_dwClassDefSize;
  32. CClassRecord* m_pParent;
  33. EKeyNess m_eIsKeyed;
  34. CPointerArray<CClassRecord> m_apChildren;
  35. bool m_bTreeComplete;
  36. bool m_bChildrenComplete;
  37. LONGLONG m_lLastChildInvalidationIndex;
  38. DWORD m_dwLastUsed;
  39. int m_nStatus;
  40. __int64 m_nClassDefCachedTime;
  41. bool m_bRead;
  42. bool m_bSystemClass;
  43. public:
  44. CClassRecord(LPCWSTR wszClassName, LPCWSTR wszHash);
  45. virtual ~CClassRecord();
  46. void AddRef() {InterlockedIncrement(&m_lRef);}
  47. void Release() {if(InterlockedDecrement(&m_lRef) == 0) delete this;}
  48. HRESULT EnsureChild(CClassRecord* pChild);
  49. HRESULT RemoveChild(CClassRecord* pChild);
  50. friend class CHierarchyCache;
  51. friend class CForestCache;
  52. };
  53. class CForestCache;
  54. class CHierarchyCache
  55. {
  56. protected:
  57. static long s_nCaches;
  58. long m_lRef;
  59. typedef std::map<LPCWSTR, CClassRecord*, wcscless> TMap;
  60. typedef TMap::iterator TIterator;
  61. CForestCache* m_pForest;
  62. CCritSec m_cs;
  63. TMap m_map;
  64. LONGLONG m_lNextInvalidationIndex;
  65. HRESULT m_hresError;
  66. public:
  67. CHierarchyCache(CForestCache* pForest);
  68. virtual ~CHierarchyCache();
  69. long AddRef() {return InterlockedIncrement(&m_lRef);}
  70. long Release() { long lRet = InterlockedDecrement(&m_lRef); if (0 == lRet) delete this; return lRet; }
  71. HRESULT AssertClass(_IWmiObject* pClass, LPCWSTR wszClassName,
  72. bool bClone, __int64 nTime, bool bSystemClass);
  73. HRESULT InvalidateClass(LPCWSTR wszClassName);
  74. HRESULT DoneWithChildren(LPCWSTR wszClassName, bool bRecursive,
  75. LONGLONG lStartingIndex,
  76. CClassRecord* pRecord = NULL);
  77. HRESULT DoneWithChildrenByHash(LPCWSTR wszHash, bool bRecursive,
  78. LONGLONG lStartIndex);
  79. LONGLONG GetLastInvalidationIndex() {return m_lNextInvalidationIndex-1;}
  80. void SetError(HRESULT hresError);
  81. HRESULT GetError();
  82. void Clear();
  83. public:
  84. RELEASE_ME _IWmiObject* GetClassDef(LPCWSTR wszClassName,
  85. bool bClone, __int64* pnTime = NULL,
  86. bool* pbRead = NULL);
  87. HRESULT EnumChildren(LPCWSTR wszClassName, bool bRecursive,
  88. CWStringArray& awsChildren);
  89. HRESULT EnumChildKeysByKey(LPCWSTR wszClassKey,
  90. CWStringArray& awsChildKeys);
  91. HRESULT GetKeyRootByKey(LPCWSTR wszKey,
  92. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  93. HRESULT GetKeyRoot(LPCWSTR wszClassName,
  94. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  95. HRESULT GetKeyRootByRecord(CClassRecord* pRecord,
  96. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  97. DELETE_ME LPWSTR GetParent(LPCWSTR wszClassName);
  98. RELEASE_ME _IWmiObject* GetClassDefByHash(LPCWSTR wszHash, bool bClone,
  99. __int64* pnTime = NULL,
  100. bool* pbRead = NULL,
  101. bool *pbSystemClass = NULL);
  102. protected:
  103. INTERNAL CClassRecord* FindClass(LPCWSTR wszClassName);
  104. INTERNAL CClassRecord* FindClassByKey(LPCWSTR wszKey);
  105. RELEASE_ME _IWmiObject* GetClassDefFromRecord(CClassRecord* pRecord,
  106. bool bClone);
  107. INTERNAL CClassRecord* EnsureClass(LPCWSTR wszClassName);
  108. HRESULT EnumChildrenInternal(CClassRecord* pRecord,
  109. bool bRecursive,
  110. CWStringArray& awsChildren);
  111. HRESULT InvalidateClassInternal(CClassRecord* pRecord);
  112. HRESULT DoneWithChildrenByRecord(CClassRecord* pRecord,
  113. bool bRecursive, LONGLONG lStartIndex);
  114. static void MakeKey(LPCWSTR wszClassName, LPWSTR wszKey);
  115. };
  116. class CForestCache
  117. {
  118. protected:
  119. CCritSec m_cs;
  120. CClassRecord* m_pMostRecentlyUsed;
  121. CClassRecord* m_pLeastRecentlyUsed;
  122. DWORD m_dwMaxMemory;
  123. DWORD m_dwMaxAgeMs;
  124. DWORD m_dwTotalMemory;
  125. //HANDLE m_hTimerQueue;
  126. HANDLE m_hCurrentTimer;
  127. //HANDLE m_hCompletionEvent;
  128. long m_lRef;
  129. BOOL m_bInit;
  130. typedef std::map<WString, CHierarchyCache*, WSiless> TMap;
  131. typedef TMap::iterator TIterator;
  132. TMap m_map;
  133. bool m_bAssertedInTransaction;
  134. public:
  135. CForestCache() : m_dwMaxMemory(0xFFFFFFFF), m_dwTotalMemory(0),
  136. m_pMostRecentlyUsed(NULL), m_pLeastRecentlyUsed(NULL),
  137. m_dwMaxAgeMs(0), m_hCurrentTimer(NULL),
  138. m_lRef(1), m_bInit(FALSE), m_bAssertedInTransaction(false)
  139. {
  140. }
  141. ~CForestCache();
  142. HRESULT Initialize();
  143. HRESULT Deinitialize();
  144. void SetMaxMemory(DWORD dwMaxMemory, DWORD dwMaxAgeMs);
  145. bool MakeRoom(DWORD dwSize);
  146. bool Flush();
  147. void MakeMostRecentlyUsed(CClassRecord* pRecord);
  148. void Add(CClassRecord* pRecord);
  149. void RemoveRecord(CClassRecord* pRecord);
  150. void BeginTransaction();
  151. bool MarkAsserted(CHierarchyCache* pCache, LPCWSTR wszClassName);
  152. void CommitTransaction();
  153. void AbortTransaction();
  154. public:
  155. CHierarchyCache* GetNamespaceCache(LPCWSTR wszNamespace);
  156. void ReleaseNamespaceCache(LPCWSTR wszNamespace, CHierarchyCache* pCache);
  157. long AddRef() { return InterlockedIncrement(&m_lRef);}
  158. long Release() { long lRet = InterlockedDecrement(&m_lRef); if (!lRet) delete this; return lRet;}
  159. protected:
  160. CCritSec* GetLock() {return &m_cs;}
  161. bool Test();
  162. void Untie(CClassRecord* pRecord);
  163. void Clear();
  164. void TimerCallback();
  165. static void staticTimerCallback(VOID* pParam, BOOLEAN);
  166. friend class CHierarchyCache;
  167. };
  168. #endif