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.5 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 <wstlallc.h>
  14. #include <lockst.h>
  15. #include "a51tools.h"
  16. class wcscless : public binary_function<LPCWSTR, LPCWSTR, bool>
  17. {
  18. public:
  19. bool operator()(const LPCWSTR& wcs1, const LPCWSTR& wcs2) const
  20. {return wcscmp(wcs1, wcs2) < 0;}
  21. };
  22. typedef enum {e_KeynessUnknown, e_Keyed, e_NotKeyed} EKeyNess;
  23. class CClassRecord
  24. {
  25. protected:
  26. static long s_nRecords;
  27. long m_lRef;
  28. CClassRecord* m_pMoreRecentlyUsed;
  29. CClassRecord* m_pLessRecentlyUsed;
  30. LPWSTR m_wszClassName;
  31. WCHAR m_wszHash[MAX_HASH_LEN+1];
  32. _IWmiObject* m_pClassDef;
  33. DWORD m_dwClassDefSize;
  34. CClassRecord* m_pParent;
  35. EKeyNess m_eIsKeyed;
  36. CPointerArray<CClassRecord> m_apChildren;
  37. bool m_bTreeComplete;
  38. bool m_bChildrenComplete;
  39. LONGLONG m_lLastChildInvalidationIndex;
  40. DWORD m_dwLastUsed;
  41. int m_nStatus;
  42. __int64 m_nClassDefCachedTime;
  43. bool m_bRead;
  44. bool m_bSystemClass;
  45. public:
  46. CClassRecord(LPCWSTR wszClassName, LPCWSTR wszHash);
  47. virtual ~CClassRecord();
  48. void AddRef() {InterlockedIncrement(&m_lRef);}
  49. void Release() {if(InterlockedDecrement(&m_lRef) == 0) delete this;}
  50. HRESULT EnsureChild(CClassRecord* pChild);
  51. HRESULT RemoveChild(CClassRecord* pChild);
  52. friend class CHierarchyCache;
  53. friend class CForestCache;
  54. };
  55. class CForestCache;
  56. class CHierarchyCache
  57. {
  58. protected:
  59. static long s_nCaches;
  60. long m_lRef;
  61. typedef std::map<LPCWSTR, CClassRecord*, wcscless, wbem_allocator<CClassRecord*> > TMap;
  62. typedef TMap::iterator TIterator;
  63. CForestCache* m_pForest;
  64. TMap m_map;
  65. LONGLONG m_lNextInvalidationIndex;
  66. HRESULT m_hresError;
  67. public:
  68. CHierarchyCache(CForestCache* pForest);
  69. virtual ~CHierarchyCache();
  70. long AddRef() {return InterlockedIncrement(&m_lRef);}
  71. long Release() { long lRet = InterlockedDecrement(&m_lRef); if (0 == lRet) delete this; return lRet; }
  72. HRESULT AssertClass(_IWmiObject* pClass, LPCWSTR wszClassName,
  73. bool bClone, __int64 nTime, bool bSystemClass);
  74. HRESULT InvalidateClass(LPCWSTR wszClassName);
  75. HRESULT DoneWithChildren(LPCWSTR wszClassName, bool bRecursive,
  76. LONGLONG lStartingIndex,
  77. CClassRecord* pRecord = NULL);
  78. HRESULT DoneWithChildrenByHash(LPCWSTR wszHash, bool bRecursive,
  79. LONGLONG lStartIndex);
  80. LONGLONG GetLastInvalidationIndex() {return m_lNextInvalidationIndex-1;}
  81. void SetError(HRESULT hresError);
  82. HRESULT GetError();
  83. void Clear();
  84. public:
  85. RELEASE_ME _IWmiObject* GetClassDef(LPCWSTR wszClassName,
  86. bool bClone, __int64* pnTime = NULL,
  87. bool* pbRead = NULL);
  88. HRESULT EnumChildren(LPCWSTR wszClassName, bool bRecursive,
  89. CWStringArray& awsChildren);
  90. HRESULT EnumChildKeysByKey(LPCWSTR wszClassKey,
  91. CWStringArray& awsChildKeys);
  92. HRESULT GetKeyRootByKey(LPCWSTR wszKey,
  93. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  94. HRESULT GetKeyRoot(LPCWSTR wszClassName,
  95. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  96. HRESULT GetKeyRootByRecord(CClassRecord* pRecord,
  97. TEMPFREE_ME LPWSTR* pwszKeyRoot);
  98. DELETE_ME LPWSTR GetParent(LPCWSTR wszClassName);
  99. RELEASE_ME _IWmiObject* GetClassDefByHash(LPCWSTR wszHash, bool bClone,
  100. __int64* pnTime = NULL,
  101. bool* pbRead = NULL,
  102. bool *pbSystemClass = NULL);
  103. protected:
  104. INTERNAL CClassRecord* FindClass(LPCWSTR wszClassName);
  105. INTERNAL CClassRecord* FindClassByKey(LPCWSTR wszKey);
  106. RELEASE_ME _IWmiObject* GetClassDefFromRecord(CClassRecord* pRecord,
  107. bool bClone);
  108. INTERNAL CClassRecord* EnsureClass(LPCWSTR wszClassName);
  109. HRESULT EnumChildrenInternal(CClassRecord* pRecord,
  110. bool bRecursive,
  111. CWStringArray& awsChildren);
  112. HRESULT InvalidateClassInternal(CClassRecord* pRecord);
  113. HRESULT DoneWithChildrenByRecord(CClassRecord* pRecord,
  114. bool bRecursive, LONGLONG lStartIndex);
  115. static void MakeKey(LPCWSTR wszClassName, LPWSTR wszKey);
  116. };
  117. class CForestCache
  118. {
  119. protected:
  120. CriticalSection m_cs;
  121. CClassRecord* m_pMostRecentlyUsed;
  122. CClassRecord* m_pLeastRecentlyUsed;
  123. DWORD m_dwMaxMemory;
  124. DWORD m_dwMaxAgeMs;
  125. DWORD m_dwTotalMemory;
  126. HANDLE m_hCurrentTimer;
  127. long m_lRef;
  128. BOOL m_bInit;
  129. typedef std::map<WString, CHierarchyCache*, WSiless, wbem_allocator<CHierarchyCache*> > TMap;
  130. typedef TMap::iterator TIterator;
  131. TMap m_map;
  132. bool m_bAssertedInTransaction;
  133. public:
  134. CForestCache() : m_dwMaxMemory(0xFFFFFFFF), m_dwTotalMemory(0),
  135. m_pMostRecentlyUsed(NULL), m_pLeastRecentlyUsed(NULL),
  136. m_dwMaxAgeMs(0), m_hCurrentTimer(NULL),
  137. m_lRef(1), m_bInit(FALSE), m_bAssertedInTransaction(false),
  138. m_cs(THROW_LOCK,0x80000000 | 400L)
  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. void Clear();
  160. protected:
  161. CriticalSection & GetLock() {return m_cs;}
  162. bool Test();
  163. void Untie(CClassRecord* pRecord);
  164. void TimerCallback();
  165. static void staticTimerCallback(VOID* pParam, BOOLEAN);
  166. friend class CHierarchyCache;
  167. };
  168. #endif