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.

263 lines
4.5 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: tls236.cpp
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. CClientMgr* g_ClientMgr=NULL;
  14. ////////////////////////////////////////////////////////
  15. //
  16. // CClient Class
  17. //
  18. ////////////////////////////////////////////////////////
  19. CClient::CClient(
  20. IN PMHANDLE hClient
  21. ) :
  22. m_hClient(hClient)
  23. /*++
  24. Abstract:
  25. Constructor for CClient class.
  26. Parameter:
  27. hClient : client handle
  28. Return:
  29. None.
  30. ++*/
  31. {
  32. }
  33. //------------------------------------------------------
  34. CClient::~CClient()
  35. /*++
  36. Abstract:
  37. Destructor for CClient class
  38. Parameter:
  39. None
  40. Return:
  41. None
  42. ++*/
  43. {
  44. for(list<PointerType>::iterator it = m_AllocatedMemory.begin();
  45. it != m_AllocatedMemory.end();
  46. it++)
  47. {
  48. HLOCAL ptr = (*it).GetPointer();
  49. FREE(ptr);
  50. }
  51. // m_AllocatedMemory.erase(m_AllocatedMemory.begin(), m_AllocatedMemory.end());
  52. }
  53. //------------------------------------------------------
  54. HLOCAL
  55. CClient::AllocateMemory(
  56. IN MEMORYPOINTERTYPE ptrType,
  57. IN DWORD dwSize
  58. )
  59. /*++
  60. Abstract:
  61. Allocate/store memory allocated into memory list.
  62. Parameter:
  63. dwSize - Number of byte to allocate.
  64. Return:
  65. Same as from LocalAlloc().
  66. ++*/
  67. {
  68. HLOCAL ptr = NULL;
  69. DWORD dwStatus = ERROR_SUCCESS;
  70. try {
  71. ptr = MALLOC(dwSize);
  72. if(ptr != NULL)
  73. {
  74. //
  75. // Append to allocated list.
  76. //
  77. m_AllocatedMemory.push_back( PointerType(ptrType, ptr) );
  78. }
  79. }
  80. catch( SE_Exception e )
  81. {
  82. SetLastError(dwStatus = e.getSeNumber());
  83. }
  84. catch( ... )
  85. {
  86. SetLastError(dwStatus = TLSA02_E_INTERNALERROR);
  87. }
  88. if(dwStatus != ERROR_SUCCESS)
  89. {
  90. if(ptr != NULL)
  91. {
  92. FREE(ptr);
  93. ptr = NULL;
  94. }
  95. }
  96. return ptr;
  97. }
  98. ////////////////////////////////////////////////////////
  99. //
  100. // CClientMgr
  101. //
  102. ////////////////////////////////////////////////////////
  103. CClientMgr::~CClientMgr()
  104. {
  105. Cleanup();
  106. }
  107. //------------------------------------------------------
  108. void
  109. CClientMgr::Cleanup()
  110. /*++
  111. ++*/
  112. {
  113. MapHandleToClient::iterator it;
  114. m_HandleMapLock.Lock();
  115. try {
  116. for(it = m_HandleMap.begin(); it != m_HandleMap.end(); it++)
  117. {
  118. assert( ((*it).second)->GetRefCount() == 1 );
  119. }
  120. }
  121. catch(...) {
  122. }
  123. m_HandleMapLock.UnLock();
  124. //
  125. // Always perform cleanup
  126. //
  127. // m_HandleMap.erase(m_HandleMap.begin(), m_HandleMap.end());
  128. }
  129. //------------------------------------------------------
  130. CClient*
  131. CClientMgr::FindClient(
  132. IN PMHANDLE hClient
  133. )
  134. /*++
  135. Abstract:
  136. Routine to find client object, add client object if not found.
  137. Parameter:
  138. hClient - Client handle
  139. Return:
  140. ++*/
  141. {
  142. EXCEPTION_RECORD ExceptionCode;
  143. MapHandleToClient::iterator it;
  144. CClient*ptr = NULL;
  145. DWORD dwStatus = ERROR_SUCCESS;
  146. m_HandleMapLock.Lock();
  147. try {
  148. it = m_HandleMap.find(hClient);
  149. if( it == m_HandleMap.end() )
  150. {
  151. CClient* pClient;
  152. pClient = new CClient(hClient);
  153. if(pClient != NULL)
  154. {
  155. m_HandleMap[hClient] = pClient;
  156. // pair<PMHANDLE, CClient*> m(hClient, pClient);
  157. //m_HandleMap.insert( m );
  158. // m_HandleMap.insert( pair<PMHANDLE, CClient*>(hClient, pClient) );
  159. it = m_HandleMap.find(hClient);
  160. assert(it != m_HandleMap.end());
  161. }
  162. }
  163. if(it != m_HandleMap.end())
  164. {
  165. ptr = (*it).second;
  166. }
  167. }
  168. catch( SE_Exception e )
  169. {
  170. dwStatus = e.getSeNumber();
  171. }
  172. catch( ... )
  173. {
  174. dwStatus = TLSA02_E_INTERNALERROR;
  175. }
  176. m_HandleMapLock.UnLock();
  177. return ptr;
  178. }
  179. //------------------------------------------------------
  180. BOOL
  181. CClientMgr::DestroyClient(
  182. IN PMHANDLE hClient
  183. )
  184. /*++
  185. ++*/
  186. {
  187. MapHandleToClient::iterator it;
  188. BOOL bSuccess = FALSE;
  189. m_HandleMapLock.Lock();
  190. try {
  191. it = m_HandleMap.find(hClient);
  192. if(it != m_HandleMap.end())
  193. {
  194. delete (*it).second;
  195. m_HandleMap.erase(it);
  196. bSuccess = TRUE;
  197. }
  198. }
  199. catch(...) {
  200. }
  201. m_HandleMapLock.UnLock();
  202. return bSuccess;
  203. }