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.

310 lines
6.3 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // objcache.h
  6. //
  7. // cvadai 19-Mar-99 Created as prototype for Quasar.
  8. //
  9. //***************************************************************************
  10. #ifndef _OBJCACHE_H_
  11. #define _OBJCACHE_H_
  12. typedef __int64 SQL_ID;
  13. #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
  14. #define NUM_BUCKETS 100
  15. //***************************************************************************
  16. // CHashCache
  17. //***************************************************************************
  18. class POLARITY CListElement
  19. {
  20. public:
  21. CListElement() {};
  22. ~CListElement() {};
  23. void *m_pObj;
  24. SQL_ID m_dId;
  25. };
  26. #define CHASHCACHE_INLINED
  27. #ifndef CHASHCACHE_INLINED
  28. class POLARITY CHashCache
  29. {
  30. typedef std::map <SQL_ID, void *> CacheInfo;
  31. public:
  32. CHashCache();
  33. ~CHashCache();
  34. HRESULT Insert(SQL_ID dId, void *pNew);
  35. HRESULT Delete(SQL_ID dId);
  36. bool Exists (SQL_ID dId);
  37. HRESULT Get(SQL_ID dId, void **ppObj);
  38. HRESULT Empty();
  39. CListElement *FindFirst();
  40. CListElement *FindNext(SQL_ID dLast);
  41. private:
  42. CacheInfo m_info;
  43. };
  44. #else
  45. class _WMILockit11
  46. {
  47. public:
  48. _WMILockit11(CRITICAL_SECTION *pCS)
  49. {
  50. EnterCriticalSection(pCS);
  51. m_cs = pCS;
  52. }
  53. ~_WMILockit11()
  54. {
  55. LeaveCriticalSection(m_cs);
  56. }
  57. private:
  58. CRITICAL_SECTION *m_cs;
  59. };
  60. template <class T>
  61. class POLARITY CHashCache
  62. {
  63. public:
  64. CHashCache();
  65. ~CHashCache();
  66. HRESULT Insert(SQL_ID dId, T pNew);
  67. HRESULT Delete(SQL_ID dId);
  68. bool Exists (SQL_ID dId);
  69. HRESULT Get(SQL_ID dId, T *ppObj);
  70. HRESULT Empty();
  71. CListElement *FindFirst();
  72. CListElement *FindNext(SQL_ID dLast);
  73. private:
  74. std::map <SQL_ID, T> m_info;
  75. CRITICAL_SECTION m_cs;
  76. };
  77. //***************************************************************************
  78. //
  79. // CHashCache::CHashCache
  80. //
  81. //***************************************************************************
  82. template <class T>
  83. CHashCache<T>::CHashCache()
  84. {
  85. InitializeCriticalSection(&m_cs);
  86. }
  87. //***************************************************************************
  88. //
  89. // CHashCache::~CHashCache
  90. //
  91. //***************************************************************************
  92. template<class T>
  93. CHashCache<T>::~CHashCache()
  94. {
  95. Empty();
  96. DeleteCriticalSection(&m_cs);
  97. }
  98. //***************************************************************************
  99. //
  100. // CHashCache::Insert
  101. //
  102. //***************************************************************************
  103. template<class T>
  104. HRESULT CHashCache<T>::Insert(SQL_ID dId, T pUnk)
  105. {
  106. _WMILockit11 _Lk(&m_cs);
  107. HRESULT hr = WBEM_S_NO_ERROR;
  108. T pTmp = m_info[dId];
  109. if (pTmp != pUnk)
  110. delete pTmp;
  111. m_info[dId] = pUnk;
  112. return hr;
  113. }
  114. //***************************************************************************
  115. //
  116. // CHashCache::Delete
  117. //
  118. //***************************************************************************
  119. template<class T>
  120. HRESULT CHashCache<T>::Delete(SQL_ID dId)
  121. {
  122. _WMILockit11 _Lk(&m_cs);
  123. HRESULT hr = WBEM_S_NO_ERROR;
  124. delete m_info[dId];
  125. m_info[dId] = NULL;
  126. std::map <SQL_ID, T>::iterator it = m_info.find(dId);
  127. if (it != m_info.end())
  128. m_info.erase(it);
  129. return hr;
  130. }
  131. //***************************************************************************
  132. //
  133. // CHashCache::Exists
  134. //
  135. //***************************************************************************
  136. template<class T>
  137. bool CHashCache<T>::Exists (SQL_ID dId)
  138. {
  139. _WMILockit11 _Lk(&m_cs);
  140. bool bRet = false;
  141. std::map <SQL_ID, T>::iterator it = m_info.find(dId);
  142. if (it != m_info.end())
  143. bRet = true;
  144. return bRet;
  145. }
  146. //***************************************************************************
  147. //
  148. // CHashCache::Get
  149. //
  150. //***************************************************************************
  151. template<class T>
  152. HRESULT CHashCache<T>::Get(SQL_ID dId, T *ppObj)
  153. {
  154. _WMILockit11 _Lk(&m_cs);
  155. HRESULT hr = WBEM_S_NO_ERROR;
  156. std::map <SQL_ID, T>::iterator it = m_info.find(dId);
  157. if (it != m_info.end())
  158. {
  159. *ppObj = (*it).second;
  160. }
  161. else
  162. hr = WBEM_E_NOT_FOUND;
  163. return hr;
  164. }
  165. //***************************************************************************
  166. //
  167. // CHashCache::FindFirst
  168. //
  169. //***************************************************************************
  170. template<class T>
  171. CListElement *CHashCache<T>::FindFirst()
  172. {
  173. _WMILockit11 _Lk(&m_cs);
  174. CListElement *pRet = NULL;
  175. std::map <SQL_ID, T>::iterator it = m_info.begin();
  176. while (it != m_info.end())
  177. {
  178. if ((*it).second)
  179. {
  180. pRet = new CListElement;
  181. if (pRet)
  182. {
  183. pRet->m_dId = (*it).first;
  184. pRet->m_pObj = (*it).second;
  185. }
  186. break;
  187. }
  188. it++;
  189. }
  190. return pRet;
  191. }
  192. //***************************************************************************
  193. //
  194. // CHashCache::FindNext
  195. //
  196. //***************************************************************************
  197. template<class T>
  198. CListElement *CHashCache<T>::FindNext(SQL_ID dId)
  199. {
  200. _WMILockit11 _Lk(&m_cs);
  201. CListElement *pRet = NULL;
  202. std::map <SQL_ID, T>::iterator it = m_info.find(dId);
  203. if (it == m_info.end())
  204. {
  205. it = m_info.begin();
  206. while (it != m_info.end() && (*it).first < dId)
  207. {
  208. it++;
  209. }
  210. }
  211. if (it != m_info.end())
  212. it++;
  213. while (it != m_info.end())
  214. {
  215. if ((*it).second)
  216. {
  217. pRet = new CListElement;
  218. if (pRet)
  219. {
  220. pRet->m_dId = (*it).first;
  221. pRet->m_pObj = (*it).second;
  222. }
  223. break;
  224. }
  225. it++;
  226. }
  227. return pRet;
  228. }
  229. //***************************************************************************
  230. //
  231. // CHashCache::Empty
  232. //
  233. //***************************************************************************
  234. template<class T>
  235. HRESULT CHashCache<T>::Empty()
  236. {
  237. _WMILockit11 _Lk(&m_cs);
  238. HRESULT hr = WBEM_S_NO_ERROR;
  239. CListElement *pRet = NULL;
  240. std::map <SQL_ID, T>::iterator it = m_info.begin();
  241. while (it != m_info.end())
  242. {
  243. delete (*it).second;
  244. it++;
  245. }
  246. m_info.clear();
  247. return hr;
  248. }
  249. #endif
  250. #endif