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.

247 lines
5.1 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // objcache.cpp
  6. //
  7. // cvadai 19-Mar-99 Created as prototype for Quasar.
  8. //
  9. //***************************************************************************
  10. #define _REPDRVR_CPP_
  11. #pragma warning( disable : 4786 ) // identifier was truncated to 'number' characters in the
  12. #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
  13. #define _WIN32_DCOM
  14. #include "precomp.h"
  15. #include <comutil.h>
  16. #include <map>
  17. #include <reposit.h>
  18. #include <wbemcli.h>
  19. #include <objcache.h>
  20. #include <corepol.h>
  21. #include <crc64.h>
  22. #ifndef CHASHCACHE_INLINED
  23. typedef std::map <SQL_ID, void *> CacheInfo;
  24. CRITICAL_SECTION g_csCache;
  25. class _WMILockit
  26. {
  27. public:
  28. _WMILockit();
  29. ~_WMILockit();
  30. };
  31. _WMILockit::_WMILockit()
  32. {
  33. EnterCriticalSection(&g_csCache);
  34. }
  35. _WMILockit::~_WMILockit()
  36. {
  37. LeaveCriticalSection(&g_csCache);
  38. }
  39. //***************************************************************************
  40. //
  41. // CHashCache::CHashCache
  42. //
  43. //***************************************************************************
  44. CHashCache::CHashCache()
  45. {
  46. if (!g_csCache.DebugInfo)
  47. InitializeCriticalSection(&g_csCache);
  48. }
  49. //***************************************************************************
  50. //
  51. // CHashCache::~CHashCache
  52. //
  53. //***************************************************************************
  54. CHashCache::~CHashCache()
  55. {
  56. Empty();
  57. if (!g_csCache.DebugInfo)
  58. DeleteCriticalSection(&g_csCache);
  59. }
  60. //***************************************************************************
  61. //
  62. // CHashCache::Insert
  63. //
  64. //***************************************************************************
  65. HRESULT CHashCache::Insert(SQL_ID dId, void *pUnk)
  66. {
  67. _WMILockit _Lk;
  68. HRESULT hr = WBEM_S_NO_ERROR;
  69. void *pTmp = m_info[dId];
  70. if (pTmp != pUnk)
  71. delete pTmp;
  72. m_info[dId] = pUnk;
  73. return hr;
  74. }
  75. //***************************************************************************
  76. //
  77. // CHashCache::Delete
  78. //
  79. //***************************************************************************
  80. HRESULT CHashCache::Delete(SQL_ID dId)
  81. {
  82. _WMILockit _Lk;
  83. HRESULT hr = WBEM_S_NO_ERROR;
  84. delete m_info[dId];
  85. m_info[dId] = NULL;
  86. CacheInfo::iterator it = m_info.find(dId);
  87. if (it != m_info.end())
  88. m_info.erase(it);
  89. return hr;
  90. }
  91. //***************************************************************************
  92. //
  93. // CHashCache::Exists
  94. //
  95. //***************************************************************************
  96. bool CHashCache::Exists (SQL_ID dId)
  97. {
  98. _WMILockit _Lk;
  99. bool bRet = false;
  100. CacheInfo::iterator it = m_info.find(dId);
  101. if (it != m_info.end())
  102. bRet = true;
  103. return bRet;
  104. }
  105. //***************************************************************************
  106. //
  107. // CHashCache::Get
  108. //
  109. //***************************************************************************
  110. HRESULT CHashCache::Get(SQL_ID dId, void **ppObj)
  111. {
  112. _WMILockit _Lk;
  113. HRESULT hr = WBEM_S_NO_ERROR;
  114. CacheInfo::iterator it = m_info.find(dId);
  115. if (it != m_info.end())
  116. {
  117. *ppObj = (*it).second;
  118. }
  119. else
  120. hr = WBEM_E_NOT_FOUND;
  121. return hr;
  122. }
  123. //***************************************************************************
  124. //
  125. // CHashCache::FindFirst
  126. //
  127. //***************************************************************************
  128. CListElement *CHashCache::FindFirst()
  129. {
  130. _WMILockit _Lk;
  131. CListElement *pRet = NULL;
  132. CacheInfo::iterator it = m_info.begin();
  133. while (it != m_info.end())
  134. {
  135. if ((*it).second)
  136. {
  137. pRet = new CListElement;
  138. if (pRet)
  139. {
  140. pRet->m_dId = (*it).first;
  141. pRet->m_pObj = (*it).second;
  142. }
  143. break;
  144. }
  145. it++;
  146. }
  147. return pRet;
  148. }
  149. //***************************************************************************
  150. //
  151. // CHashCache::FindNext
  152. //
  153. //***************************************************************************
  154. CListElement *CHashCache::FindNext(SQL_ID dId)
  155. {
  156. _WMILockit _Lk;
  157. CListElement *pRet = NULL;
  158. CacheInfo::iterator it = m_info.find(dId);
  159. if (it == m_info.end())
  160. {
  161. it = m_info.begin();
  162. while ((*it).first < dId)
  163. {
  164. it++;
  165. if (it == m_info.end())
  166. break;
  167. }
  168. }
  169. if (it != m_info.end())
  170. it++;
  171. while (it != m_info.end())
  172. {
  173. if ((*it).second)
  174. {
  175. pRet = new CListElement;
  176. if (pRet)
  177. {
  178. pRet->m_dId = (*it).first;
  179. pRet->m_pObj = (*it).second;
  180. }
  181. break;
  182. }
  183. it++;
  184. }
  185. return pRet;
  186. }
  187. //***************************************************************************
  188. //
  189. // CHashCache::Empty
  190. //
  191. //***************************************************************************
  192. HRESULT CHashCache::Empty()
  193. {
  194. _WMILockit _Lk;
  195. HRESULT hr = WBEM_S_NO_ERROR;
  196. m_info.clear();
  197. return hr;
  198. }
  199. #endif