Leaked source code of windows server 2003
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.

357 lines
11 KiB

  1. /*-----------------------------------------------------------------------------
  2. *
  3. * File: wiacache.cpp
  4. * Author: Samuel Clement (samclem)
  5. * Date: Thu Sep 09 16:15:11 1999
  6. *
  7. * Copyright (c) 1999 Microsoft Corporation
  8. *
  9. * Description:
  10. *
  11. * This contains the implementation of the CWiaCacheManager. Which handles
  12. * managing items/data that we want to cache for performance reasons
  13. *
  14. * History:
  15. * 09 Sep 1999: Created.
  16. *----------------------------------------------------------------------------*/
  17. #include "stdafx.h"
  18. DeclareTag( tagWiaCache, "!WiaCache", "Wia cache debug information" );
  19. CWiaCacheManager* CWiaCacheManager::sm_pManagerInstance = NULL;
  20. /*-----------------------------------------------------------------------------
  21. * CWiaCacheManager
  22. *
  23. * This creates a new CWiaCacheManager object. This simply initalizes all
  24. * the variables to a known state. Initialize handles actually creating
  25. * the objects that we need.
  26. *--(samclem)-----------------------------------------------------------------*/
  27. CWiaCacheManager::CWiaCacheManager() : m_hThumbHeap( 0 )
  28. {
  29. TraceTag((0,"** Creating WiaCache" ));
  30. TRACK_OBJECT( "CWiaCacheManager - SINGLETON" );
  31. }
  32. /*-----------------------------------------------------------------------------
  33. * ~CWiaCacheManager
  34. *
  35. * This destroyes the CWiaCacheManager object. This will handle taking all
  36. * the memory it has away with it. including any thumbnail memory we might be
  37. * carrying around.
  38. *--(samclem)-----------------------------------------------------------------*/
  39. CWiaCacheManager::~CWiaCacheManager()
  40. {
  41. TraceTag((0, "** Destroying WiaCache" ));
  42. // Destroying the heap will free any memory
  43. // allocated by the heap, so this will keep us
  44. // from leaking
  45. if ( m_hThumbHeap )
  46. {
  47. HeapDestroy( m_hThumbHeap );
  48. m_hThumbHeap = 0;
  49. }
  50. // destroy our critical section
  51. DeleteCriticalSection( &m_cs );
  52. }
  53. /*-----------------------------------------------------------------------------
  54. * CWiaCacheManager::Initialize
  55. *
  56. * This is called following a call to new in order to get this object ready
  57. * to use. Without calling this the cache manager will be unusable.
  58. *--(samclem)-----------------------------------------------------------------*/
  59. bool CWiaCacheManager::Initialize()
  60. {
  61. SYSTEM_INFO si;
  62. // initialize our critical section
  63. __try {
  64. if(!InitializeCriticalSectionAndSpinCount( &m_cs, MINLONG )) {
  65. return false;
  66. }
  67. }
  68. __except(EXCEPTION_EXECUTE_HANDLER) {
  69. return false;
  70. }
  71. // we need to create the heap we are going to allocate
  72. // out thumbnail memory from
  73. GetSystemInfo( &si );
  74. m_hThumbHeap = TW32( HeapCreate( HEAP_NO_SERIALIZE, si.dwPageSize, 0 ), HANDLE(0) );
  75. return ( m_hThumbHeap != 0 );
  76. }
  77. /*-----------------------------------------------------------------------------
  78. * CWiaCacheManager::GetDevice
  79. *
  80. * This returns a cached pointer to the device. This returns true if the
  81. * device was found, (and the out param is valid) or false if we didn't
  82. * find it. Example:
  83. *
  84. * CWiaCacheManager* pCache = CWiaCacheManager::GetInstance();
  85. * IWiaItem* pItem = NULL;
  86. *
  87. * if ( pCache->GetDevice( bstrId, &pItem ) )
  88. * {
  89. * // use pItem
  90. * .
  91. * .
  92. * pItem->Release();
  93. * }
  94. * else
  95. * {
  96. * // create pItem and use
  97. * .
  98. * .
  99. * pCache->AddDevice( bstrId, pItem );
  100. * pItem->Release();
  101. * }
  102. *
  103. * bstrId: the id of the device that we want.
  104. * ppDevice: Out, recieves the cached device pointer.
  105. *--(samclem)-----------------------------------------------------------------*/
  106. bool CWiaCacheManager::GetDevice( CComBSTR bstrId, IWiaItem** ppDevice )
  107. {
  108. return FALSE;
  109. //
  110. // NOTE: We've effectively disabled the device cache by always returning
  111. // FALSE here and not calling AddDevice anywhere else.
  112. //
  113. /*
  114. bool fRet = true;
  115. Assert( ppDevice );
  116. // our member class does most of the work for us, however we need
  117. // to ensure that we are thread safe
  118. Lock();
  119. *ppDevice = m_icItemCache.GetFromCache( bstrId );
  120. Unlock();
  121. if ( !(*ppDevice) )
  122. fRet = false;
  123. else
  124. (*ppDevice)->AddRef();
  125. return fRet;
  126. */
  127. }
  128. /*-----------------------------------------------------------------------------
  129. * CWiaCacheManager::AddDevice
  130. *
  131. * This adds a device item pointer to the cache. See GetDevice() for an
  132. * complete example. Returns true if we successfully added the device
  133. *
  134. * bstrId: the ID of the new device to add
  135. * pDevice: The pointer to add to the cache
  136. *--(samclem)-----------------------------------------------------------------*/
  137. bool CWiaCacheManager::AddDevice( CComBSTR bstrId, IWiaItem* pDevice )
  138. {
  139. bool fRet = true;
  140. Assert( pDevice );
  141. // again our member class does a majority of the work so all we
  142. // need to do is to call through. However, we make sure that
  143. // we are thread safe when we do that
  144. Lock();
  145. fRet = m_icItemCache.AddToCache( bstrId, pDevice );
  146. Unlock();
  147. return fRet;
  148. }
  149. /*-----------------------------------------------------------------------------
  150. * CWiaCacheManager::RemoveDevice
  151. *
  152. * This removes a device from the cache. This returns true of the item
  153. * was found in the cache, or false if it was not.
  154. *
  155. * bstrId: the ID of the device to remove from the cache
  156. *--(samclem)-----------------------------------------------------------------*/
  157. bool CWiaCacheManager::RemoveDevice( CComBSTR bstrId )
  158. {
  159. bool fRet = false;
  160. Lock();
  161. // remove the item from the cache
  162. fRet = m_icItemCache.RemoveFromCache( bstrId );
  163. Unlock();
  164. return fRet;
  165. }
  166. /*-----------------------------------------------------------------------------
  167. * CWiaCacheManager::GetThumbnail
  168. *
  169. * This attempts to get a thumbnail from the cache. There might not
  170. * be one there, in which case this simply returns 'false'
  171. *
  172. * bstrFullItemName: the name of the item we want the thumb for
  173. * ppbThumb: Out, pointer to the thumbnail bites
  174. * pcbThumb: Out, recieves the size of the thumbnail
  175. *--(samclem)-----------------------------------------------------------------*/
  176. bool CWiaCacheManager::GetThumbnail( CComBSTR bstrFullItemName, BYTE** ppbThumb, DWORD* pcbThumb )
  177. {
  178. bool fRet = false;
  179. THUMBNAILCACHEITEM* ptci = 0;
  180. Assert( ppbThumb && pcbThumb );
  181. *ppbThumb = 0;
  182. *pcbThumb = 0;
  183. Lock();
  184. ptci = m_tcThumbnails[bstrFullItemName];
  185. if ( ptci )
  186. {
  187. *ppbThumb = ptci->pbThumb;
  188. *pcbThumb = ptci->cbThumb;
  189. fRet = true;
  190. }
  191. Unlock();
  192. return fRet;
  193. }
  194. /*-----------------------------------------------------------------------------
  195. * CWiaCacheManager::AddThumbnail
  196. *
  197. * This attempts to add a thumbnail to the cache. this will return true
  198. * if the item was successfully added or false if it failed.
  199. *
  200. * bstrFullItemName: the name of the item to cache the thumb for
  201. * pbThumb: Pointer to the thumbnail memory
  202. * cbThumb: the number of bytes in the thumbnail.
  203. *--(samclem)-----------------------------------------------------------------*/
  204. bool CWiaCacheManager::AddThumbnail( CComBSTR bstrFullItemName, BYTE* pbThumb, DWORD cbThumb )
  205. {
  206. bool fRet = false;
  207. THUMBNAILCACHEITEM* ptci = 0;
  208. Assert( pbThumb && cbThumb );
  209. Assert( m_hThumbHeap && "Need a valid thumbnail heap" );
  210. RemoveThumbnail( bstrFullItemName );
  211. Lock();
  212. ptci = reinterpret_cast<THUMBNAILCACHEITEM*>(TW32(HeapAlloc( m_hThumbHeap,
  213. HEAP_NO_SERIALIZE, sizeof( THUMBNAILCACHEITEM ) ), LPVOID(0) ) );
  214. if ( ptci )
  215. {
  216. ptci->pbThumb = pbThumb;
  217. ptci->cbThumb = cbThumb;
  218. m_tcThumbnails[bstrFullItemName] = ptci;
  219. fRet = true;
  220. }
  221. Unlock();
  222. return fRet;
  223. }
  224. /*-----------------------------------------------------------------------------
  225. * CWiaCacheManager::RemoveThumbnail
  226. *
  227. * THis removes a thumbnail from the cache. This will return true if found
  228. * an item to remove, or false if it removed nothing.
  229. *
  230. * bstrFullItemName: the name of the item to remove from the cache.
  231. *--(samclem)-----------------------------------------------------------------*/
  232. bool CWiaCacheManager::RemoveThumbnail( CComBSTR bstrFullItemName )
  233. {
  234. bool fRet = false;
  235. THUMBNAILCACHEITEM* ptci = 0;
  236. Lock();
  237. ptci = m_tcThumbnails[bstrFullItemName];
  238. if ( ptci )
  239. {
  240. m_tcThumbnails.erase( bstrFullItemName );
  241. TW32( HeapFree( m_hThumbHeap, HEAP_NO_SERIALIZE, ptci ), FALSE );
  242. fRet = true;
  243. }
  244. Unlock();
  245. return fRet;
  246. }
  247. /*-----------------------------------------------------------------------------
  248. * CWiaCacheManager::AllocThumbnail
  249. *
  250. * This allocates memory for a thumbnail from our thumbnail heap.
  251. *
  252. * cbThumb: the size of the thumbnail to allocate
  253. * ppbThumb: Out, recieves the pointer to the memory
  254. *--(samclem)-----------------------------------------------------------------*/
  255. bool CWiaCacheManager::AllocThumbnail( DWORD cbThumb, BYTE** ppbThumb )
  256. {
  257. Assert( m_hThumbHeap && "Error: NULL thumbnail heap" );
  258. Assert( ppbThumb && cbThumb != 0 );
  259. Lock();
  260. *ppbThumb = reinterpret_cast<BYTE*>(TW32( HeapAlloc( m_hThumbHeap,
  261. HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY, cbThumb ), LPVOID(0) ));
  262. Unlock();
  263. return ( *ppbThumb != NULL );
  264. }
  265. /*-----------------------------------------------------------------------------
  266. * CWiaCacheManager::FreeThumbnail
  267. *
  268. * This frees the thumbnail memory. This _SHOULD_NOT_ be called if the
  269. * thumbnail is cached. This should only be called to cleanup after an error
  270. * generating the thumbnail.
  271. *
  272. * pbThumb: the memory to free
  273. *--(samclem)-----------------------------------------------------------------*/
  274. void CWiaCacheManager::FreeThumbnail( BYTE* pbThumb )
  275. {
  276. Assert( m_hThumbHeap && "Error: NULL thumbnail heap" );
  277. Assert( pbThumb );
  278. Lock();
  279. TW32( HeapFree( m_hThumbHeap, HEAP_NO_SERIALIZE, pbThumb ), FALSE );
  280. Unlock();
  281. }
  282. /*-----------------------------------------------------------------------------
  283. * CWiaCacheManager::Init [static]
  284. *
  285. * This is called to initalize the cache manager. This simply creates and
  286. * instance of the cache manager and then initalizes it.
  287. *
  288. * Notes: This can only be called once
  289. *--(samclem)-----------------------------------------------------------------*/
  290. bool CWiaCacheManager::Init()
  291. {
  292. Assert( !sm_pManagerInstance &&
  293. "\nInit() can only be called once. Expected NULL instance" );
  294. sm_pManagerInstance = new CWiaCacheManager();
  295. if ( !sm_pManagerInstance )
  296. return false;
  297. return sm_pManagerInstance->Initialize();
  298. }
  299. /*-----------------------------------------------------------------------------
  300. * CWiaCacheManager::Uninit [static]
  301. *
  302. * This is called to uninitialize the cache manager. basically this is called
  303. * to destroy the instance we have. If we have one.
  304. *
  305. * Notes: This should only be called once
  306. *--(samclem)-----------------------------------------------------------------*/
  307. bool CWiaCacheManager::Uninit()
  308. {
  309. if ( sm_pManagerInstance )
  310. delete sm_pManagerInstance;
  311. sm_pManagerInstance = 0;
  312. return true;
  313. }