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.

223 lines
5.8 KiB

  1. /////////////////////////////////////////////////////////////////////////
  2. //
  3. // Nt4SvcToResMap.cpp
  4. //
  5. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. // History: 10/15/97 Sanj Created by Sanj
  8. // 10/17/97 jennymc Moved things a tiny bit
  9. //
  10. /////////////////////////////////////////////////////////////////////////
  11. #include "precomp.h"
  12. #include <assertbreak.h>
  13. #include <cregcls.h>
  14. #include "ntdevtosvcsearch.h"
  15. #include "nt4svctoresmap.h"
  16. #ifdef NTONLY
  17. // The Map we use to back up this class is an STL Template, so make sure we have the
  18. // std namespace available to us.
  19. using namespace std;
  20. CNT4ServiceToResourceMap::CNT4ServiceToResourceMap( void )
  21. : m_HardwareResource(),
  22. m_map()
  23. {
  24. InitializeMap();
  25. }
  26. CNT4ServiceToResourceMap::~CNT4ServiceToResourceMap( void )
  27. {
  28. Clear();
  29. }
  30. BOOL CNT4ServiceToResourceMap::InitializeMap( void )
  31. {
  32. BOOL fReturn = FALSE;
  33. //=======================================
  34. // Create hardware system resource list &
  35. // get the head of the list
  36. //=======================================
  37. m_HardwareResource.CreateSystemResourceLists();
  38. if ( WalkResourceNodes( m_HardwareResource._SystemResourceList.InterruptHead ) )
  39. {
  40. if ( WalkResourceNodes( m_HardwareResource._SystemResourceList.PortHead ) )
  41. {
  42. if ( WalkResourceNodes( m_HardwareResource._SystemResourceList.MemoryHead ) )
  43. {
  44. fReturn = WalkResourceNodes( m_HardwareResource._SystemResourceList.DmaHead );
  45. }
  46. }
  47. }
  48. return fReturn;
  49. }
  50. BOOL CNT4ServiceToResourceMap::WalkResourceNodes( LPRESOURCE_DESCRIPTOR pResourceDescriptor )
  51. {
  52. CNTDeviceToServiceSearch devSearch;
  53. CHString strOwnerServiceName;
  54. BOOL fReturn = TRUE;
  55. NT4SvcToResourceMapIter mapIter;
  56. // For each descriptor we find, get the resource owner, then convert the name (if
  57. // it is not a HAL resource) to an NT service name. From there, if the name does
  58. // not already exist in the map, we need to allocate a new array, otherwise, get
  59. // the existing pointer. Then add the resource descriptor to the array, so we end
  60. // up with a structure where a service name will get us to a list of resources owned
  61. // by said service.
  62. while ( NULL != pResourceDescriptor && fReturn )
  63. {
  64. // if ( !strstr( pResourceDescriptor->Owner->Name,"HAL")
  65. // && devSearch.Find( pResourceDescriptor->Owner->Name, strOwnerServiceName ) )
  66. // {
  67. // Because the CHString compare is case sensitive, and the names
  68. // of our services as we retrieve them are not necessarily so,
  69. // we uppercase everything so we are theoretically forcing
  70. // case insensitivity.
  71. // Before we used to do an (expensive) scan of the registry. Now,
  72. // I store the registry key in the resource structure.
  73. strOwnerServiceName.Empty();
  74. CHString sParse(pResourceDescriptor->Owner->KeyName);
  75. // Parse off the last part of the registry key name
  76. int iWhere = sParse.ReverseFind(_T('\\'));
  77. if (iWhere != -1)
  78. {
  79. strOwnerServiceName = sParse.Mid(iWhere + 1);
  80. }
  81. else
  82. {
  83. // If something went wrong, fall back to the other way
  84. devSearch.Find( pResourceDescriptor->Owner->Name, strOwnerServiceName);
  85. ASSERT_BREAK(0);
  86. }
  87. if (!strOwnerServiceName.IsEmpty())
  88. {
  89. strOwnerServiceName.MakeUpper();
  90. CHPtrArray* pPtrArray = NULL;
  91. if( ( mapIter = m_map.find( strOwnerServiceName ) ) == m_map.end() )
  92. {
  93. pPtrArray = new CHPtrArray;
  94. if (pPtrArray == NULL)
  95. {
  96. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  97. }
  98. try
  99. {
  100. m_map[strOwnerServiceName] = pPtrArray;
  101. }
  102. catch ( ... )
  103. {
  104. delete pPtrArray;
  105. throw ;
  106. }
  107. }
  108. else
  109. {
  110. pPtrArray = mapIter->second;
  111. }
  112. if ( NULL != pPtrArray )
  113. {
  114. pPtrArray->Add( pResourceDescriptor );
  115. }
  116. else
  117. {
  118. fReturn = FALSE;
  119. }
  120. } // If owner generated a valid service name
  121. pResourceDescriptor = pResourceDescriptor->NextSame;
  122. }
  123. return fReturn;
  124. }
  125. DWORD CNT4ServiceToResourceMap::NumServiceResources( LPCTSTR pszServiceName )
  126. {
  127. DWORD dwNumResources = 0;
  128. NT4SvcToResourceMapIter mapIter;
  129. // Upper case for case-insensitivity
  130. CHString strUpperCaseServiceName( pszServiceName );
  131. strUpperCaseServiceName.MakeUpper();
  132. if( ( mapIter = m_map.find( strUpperCaseServiceName ) ) != m_map.end() )
  133. {
  134. CHPtrArray* pResources = mapIter->second;
  135. if ( NULL != pResources )
  136. {
  137. dwNumResources = pResources->GetSize();
  138. }
  139. }
  140. return dwNumResources;
  141. }
  142. LPRESOURCE_DESCRIPTOR CNT4ServiceToResourceMap::GetServiceResource( LPCTSTR pszServiceName, DWORD dwIndex )
  143. {
  144. LPRESOURCE_DESCRIPTOR pResourceDescriptor = NULL;
  145. NT4SvcToResourceMapIter mapIter;
  146. // Upper case for case-insensitivity
  147. CHString strUpperCaseServiceName( pszServiceName );
  148. strUpperCaseServiceName.MakeUpper();
  149. if( ( mapIter = m_map.find( strUpperCaseServiceName ) ) != m_map.end() )
  150. {
  151. CHPtrArray* pResources = mapIter->second;
  152. if ( NULL != pResources
  153. && dwIndex < pResources->GetSize() )
  154. {
  155. pResourceDescriptor = (LPRESOURCE_DESCRIPTOR) pResources->GetAt( dwIndex );
  156. }
  157. }
  158. return pResourceDescriptor;
  159. }
  160. void CNT4ServiceToResourceMap::Clear( void )
  161. {
  162. CHPtrArray* pPtrArray = NULL;
  163. // Delete all list entries and then clear out the list.
  164. for ( NT4SvcToResourceMapIter mapIter = m_map.begin();
  165. mapIter != m_map.end();
  166. mapIter++ )
  167. {
  168. pPtrArray = mapIter->second;
  169. if ( NULL != pPtrArray )
  170. {
  171. delete pPtrArray;
  172. }
  173. }
  174. m_map.erase( m_map.begin(), m_map.end() );
  175. }
  176. #endif