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.

246 lines
6.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 2000.
  5. //
  6. // File: XlatStat.cxx
  7. //
  8. // Contents: State translation class.
  9. //
  10. // Classes: CXlatState
  11. //
  12. // History: 01-20-92 KyleP Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include <xlatstat.hxx>
  18. #include "stateset.hxx"
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Member: CXlatState::CXlatState, public
  22. //
  23. // Synopsis: Initialize state translator (no states)
  24. //
  25. // Arguments: [MaxState] -- Largest ordinal of any state that may
  26. // appear in an equivalence class.
  27. //
  28. // History: 20-Jan-92 KyleP Created
  29. //
  30. //--------------------------------------------------------------------------
  31. CXlatState::CXlatState( UINT MaxState )
  32. : _cUsed( 0 ),
  33. _pulStates( 0 ),
  34. _cStates( 0 )
  35. {
  36. //
  37. // Compute the number of DWords / state, assuring at least 1 dword is
  38. // always allocated.
  39. //
  40. _StateSize = (MaxState + sizeof(ULONG) * 8 ) / (sizeof( ULONG ) * 8);
  41. _Realloc();
  42. };
  43. //+-------------------------------------------------------------------------
  44. //
  45. // Member: CXlatState::~CXlatState, public
  46. //
  47. // Synopsis: Destroys class.
  48. //
  49. // History: 20-Jan-92 KyleP Created
  50. //
  51. //--------------------------------------------------------------------------
  52. CXlatState::~CXlatState()
  53. {
  54. delete _pulStates;
  55. }
  56. //+-------------------------------------------------------------------------
  57. //
  58. // Member: CXlatState::XlatToOne, public
  59. //
  60. // Synopsis: Maps a state set to its single state equivalent. If
  61. // there is no mapping, then one is created.
  62. //
  63. // Arguments: [ss] -- State set to search for.
  64. //
  65. // Returns: The single state equivalent.
  66. //
  67. // History: 20-Jun-92 KyleP Created
  68. //
  69. //--------------------------------------------------------------------------
  70. UINT CXlatState::XlatToOne( CStateSet const & ss )
  71. {
  72. UINT state = _Search( ss );
  73. if ( state == 0 )
  74. {
  75. state = _Create( ss );
  76. }
  77. return( state );
  78. }
  79. //+-------------------------------------------------------------------------
  80. //
  81. // Member: CXlatState::XlatToMany, public
  82. //
  83. // Synopsis: Maps a state to its matching state set.
  84. //
  85. // Arguments: [iState] -- State to map.
  86. // [ss] -- On return contains the mapped state set.
  87. //
  88. // History: 20-Jan-92 KyleP Created
  89. //
  90. //--------------------------------------------------------------------------
  91. void CXlatState::XlatToMany( UINT iState, CStateSet & ss )
  92. {
  93. if ( iState > _cUsed )
  94. {
  95. vqAssert( FALSE );
  96. return; // Don't know about this state.
  97. }
  98. ULONG * pState = _pulStates + iState * _StateSize;
  99. for ( int i = _StateSize * sizeof(ULONG) * 8 - 1; i >= 0; i-- )
  100. {
  101. if ( pState[i / (sizeof(ULONG) * 8) ] &
  102. ( 1L << i % (sizeof(ULONG) * 8 ) ) )
  103. {
  104. ss.Add( i+1 );
  105. }
  106. }
  107. }
  108. //+-------------------------------------------------------------------------
  109. //
  110. // Member: CXlatState::_Search, private
  111. //
  112. // Arguments: [ss] -- State set to search for.
  113. //
  114. // Returns: Single state mapping for [ss] or 0 if none.
  115. //
  116. // History: 20-Jan-92 KyleP Created
  117. //
  118. // Notes: The first state set is used as temp space.
  119. //
  120. //--------------------------------------------------------------------------
  121. UINT CXlatState::_Search( CStateSet const & ss )
  122. {
  123. memset( _pulStates, 0, _StateSize * sizeof(ULONG) );
  124. _BuildState( _pulStates, ss );
  125. UINT cState = 1;
  126. while ( cState <= _cUsed )
  127. {
  128. if ( memcmp( _pulStates + cState * _StateSize,
  129. _pulStates,
  130. _StateSize * sizeof(ULONG) ) == 0 )
  131. {
  132. return( cState );
  133. }
  134. else
  135. {
  136. ++cState;
  137. }
  138. }
  139. return( 0 );
  140. }
  141. //+-------------------------------------------------------------------------
  142. //
  143. // Member: CXlatState::_Create, private
  144. //
  145. // Synopsis: Adds new state set to array.
  146. //
  147. // Arguments: [ss] -- State set to add.
  148. //
  149. // Returns: Single state mapping for [ss]
  150. //
  151. // History: 20-Jan-92 KyleP Created
  152. //
  153. //--------------------------------------------------------------------------
  154. UINT CXlatState::_Create( CStateSet const & ss )
  155. {
  156. //
  157. // _cStates-1 because the first state is temp space.
  158. //
  159. if ( _cStates-1 == _cUsed )
  160. _Realloc();
  161. ++_cUsed;
  162. _BuildState( _pulStates + _cUsed * _StateSize, ss );
  163. return( _cUsed );
  164. }
  165. //+-------------------------------------------------------------------------
  166. //
  167. // Member: CXlatState::_BuildState, private
  168. //
  169. // Synopsis: Formats state set.
  170. //
  171. // Arguments: [ss] -- Format state set...
  172. // [pState] -- into this memory.
  173. //
  174. // History: 20-Jan-92 KyleP Created
  175. //
  176. //--------------------------------------------------------------------------
  177. void CXlatState::_BuildState( ULONG * pState, CStateSet const & ss )
  178. {
  179. for ( UINT i = ss.Count(); i > 0; i-- )
  180. {
  181. UINT StateNum = ss.State( i ) - 1;
  182. pState[ StateNum / (sizeof(ULONG) * 8) ] |=
  183. 1L << StateNum % (sizeof(ULONG) * 8);
  184. }
  185. }
  186. //+-------------------------------------------------------------------------
  187. //
  188. // Member: CXlatState::_Realloc, private
  189. //
  190. // Synopsis: Grows state set array.
  191. //
  192. // History: 20-Jan-92 KyleP Created
  193. //
  194. // Notes: Grows linearly, since the number of state sets is likely
  195. // to be small and we don't want to waste space.
  196. //
  197. //--------------------------------------------------------------------------
  198. void CXlatState::_Realloc()
  199. {
  200. UINT oldcStates = _cStates;
  201. ULONG * oldpulStates = _pulStates;
  202. _cStates += 10;
  203. _pulStates = new ULONG [ _cStates * _StateSize ];
  204. memcpy( _pulStates,
  205. oldpulStates,
  206. oldcStates * _StateSize * sizeof( ULONG ) );
  207. memset( _pulStates + oldcStates * _StateSize,
  208. 0,
  209. (_cStates - oldcStates) * _StateSize * sizeof(ULONG) );
  210. delete [] oldpulStates;
  211. }