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.

279 lines
6.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1995 - 1998
  5. //
  6. // File: RowMan.hxx
  7. //
  8. // Contents: Distributed HROW manager.
  9. //
  10. // Classes: CHRowManager
  11. //
  12. // History: 05-Jun-95 KyleP Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Class: CHRowManager
  19. //
  20. // Purpose: Distributed HROW manager.
  21. //
  22. // History: 05-Jun-95 KyleP Created.
  23. //
  24. //----------------------------------------------------------------------------
  25. class CHRowManager
  26. {
  27. public:
  28. CHRowManager();
  29. ~CHRowManager();
  30. //
  31. // For bookmark hints
  32. //
  33. void TrackSiblings( unsigned cChild );
  34. inline BOOL IsTrackingSiblings();
  35. //
  36. // Set modification
  37. //
  38. HROW Add( unsigned iChild, HROW hrow );
  39. HROW Add( unsigned iChild, HROW const * ahrow );
  40. void AddRef( HROW hrow );
  41. void Release( HROW hrow );
  42. //
  43. // Row access
  44. //
  45. inline HROW ChildHROW( HROW hrow );
  46. inline unsigned GetChildAndHROW( HROW hrow, HROW & hrowChild );
  47. HROW * GetChildAndHROWs( HROW hrow, unsigned & iChild );
  48. BOOL IsSame( HROW hrow1, HROW hrow2 );
  49. private:
  50. inline unsigned ConvertAndValidate( HROW hrow );
  51. inline int InnerAdd( unsigned iChild, HROW hrow );
  52. void Grow();
  53. //
  54. // Tracks single row
  55. //
  56. class CHRow
  57. {
  58. public:
  59. CHRow() : _cRef(0) {}
  60. void Init( unsigned iChild, HROW hrow )
  61. {
  62. Win4Assert( !IsInUse() );
  63. _cRef = 1;
  64. _iChild = iChild;
  65. _hrow = hrow;
  66. }
  67. //
  68. // Refcounting
  69. //
  70. void AddRef() { _cRef++; }
  71. void Release() { _cRef--; }
  72. BOOL IsInUse() { return _cRef > 0; }
  73. //
  74. // Member access
  75. //
  76. int Child() { return _iChild; }
  77. HROW HRow() { return _hrow; }
  78. //
  79. // Linking
  80. //
  81. void Link( int iNext )
  82. {
  83. Win4Assert( _cRef == 0 );
  84. _iChild = iNext;
  85. }
  86. int Next()
  87. {
  88. Win4Assert( _cRef == 0 );
  89. return _iChild;
  90. }
  91. private:
  92. unsigned _cRef;
  93. unsigned _iChild;
  94. HROW _hrow;
  95. };
  96. //
  97. // Dynamic array of HROW, linked into a free list.
  98. //
  99. CHRow * _aHRow;
  100. unsigned _cHRow;
  101. int _iFirstFree;
  102. //
  103. // For tracking siblings. Two dimensional array: _cChild x _cHRow.
  104. //
  105. HROW * _ahrowHint;
  106. unsigned _cChild;
  107. };
  108. //+---------------------------------------------------------------------------
  109. //
  110. // Member: CHRowManager::IsTrackingSiblings, public
  111. //
  112. // Returns: TRUE if the row manager is tracking HROW 'hints' for other
  113. // cursors.
  114. //
  115. // History: 05-Jun-95 KyleP Created.
  116. //
  117. //----------------------------------------------------------------------------
  118. inline BOOL CHRowManager::IsTrackingSiblings()
  119. {
  120. return( 0 != _ahrowHint );
  121. }
  122. //+---------------------------------------------------------------------------
  123. //
  124. // Member: CHRowManager::ChildHROW, public
  125. //
  126. // Arguments: [hrow] -- Distributed HROW
  127. //
  128. // Returns: HROW used by child cursor.
  129. //
  130. // History: 05-Jun-95 KyleP Created.
  131. //
  132. //----------------------------------------------------------------------------
  133. inline HROW CHRowManager::ChildHROW( HROW hrow )
  134. {
  135. unsigned iRow = ConvertAndValidate( hrow );
  136. return( _aHRow[iRow].HRow() );
  137. }
  138. //+---------------------------------------------------------------------------
  139. //
  140. // Member: CHRowManager::GetChildAndHROW, public
  141. //
  142. // Arguments: [hrow] -- Distributed HROW
  143. // [hrowChild] -- HROW used by child cursor returned here.
  144. //
  145. // Returns: Index of child cursor for [hrow].
  146. //
  147. // History: 05-Jun-95 KyleP Created.
  148. //
  149. //----------------------------------------------------------------------------
  150. inline unsigned CHRowManager::GetChildAndHROW( HROW hrow, HROW & hrowChild )
  151. {
  152. unsigned iRow = ConvertAndValidate( hrow );
  153. hrowChild = _aHRow[iRow].HRow();
  154. return _aHRow[iRow].Child();
  155. }
  156. //+---------------------------------------------------------------------------
  157. //
  158. // Member: CHRowManager::GetChildAndHROWs, public
  159. //
  160. // Arguments: [hrow] -- Distributed HROW
  161. // [iChild] -- Index of child cursor returned here.
  162. //
  163. // Returns: Array of HROW 'hints', one per cursor. The [iChild] element
  164. // is not a hint. It is the HROW for the owning cursor.
  165. //
  166. // History: 05-Jun-95 KyleP Created.
  167. //
  168. //----------------------------------------------------------------------------
  169. inline HROW * CHRowManager::GetChildAndHROWs( HROW hrow, unsigned & iChild )
  170. {
  171. Win4Assert( IsTrackingSiblings() );
  172. unsigned iRow = ConvertAndValidate( hrow );
  173. iChild = _aHRow[iRow].Child();
  174. return _ahrowHint + iRow * _cChild;
  175. }
  176. //+---------------------------------------------------------------------------
  177. //
  178. // Member: CHRowManager::ConvertAndValidate, public
  179. //
  180. // Arguments: [hrow] -- Distributed HROW
  181. //
  182. // Returns: Index into _aHRow of row.
  183. //
  184. // History: 05-Jun-95 KyleP Created.
  185. //
  186. //----------------------------------------------------------------------------
  187. inline unsigned CHRowManager::ConvertAndValidate( HROW hrow )
  188. {
  189. unsigned iRow = (unsigned)hrow - 1; // we are 0 based while hrow is 1 based
  190. if ( iRow >= _cHRow || !_aHRow[iRow].IsInUse() )
  191. {
  192. Win4Assert( !"Invalid HROW" );
  193. vqDebugOut(( DEB_ERROR, "Invalid HROW 0x%x\n", iRow ));
  194. THROW( CException( DB_E_BADROWHANDLE ) );
  195. }
  196. return iRow;
  197. }
  198. //+---------------------------------------------------------------------------
  199. //
  200. // Member: CHRowManager::InnerAdd, public
  201. //
  202. // Synopsis: The guts of Add*
  203. //
  204. // Arguments: [iChild] -- Index of governing child cursor.
  205. // [hrow] -- HROW used by child cursor
  206. //
  207. // Returns: Index of row in _aHRow
  208. //
  209. // History: 05-Jun-95 KyleP Created.
  210. //
  211. //----------------------------------------------------------------------------
  212. inline int CHRowManager::InnerAdd( unsigned iChild, HROW hrow )
  213. {
  214. if ( _iFirstFree == -1 )
  215. Grow();
  216. Win4Assert( _iFirstFree != -1 );
  217. int iCurrent = _iFirstFree;
  218. _iFirstFree = _aHRow[iCurrent].Next();
  219. _aHRow[iCurrent].Init( iChild, hrow );
  220. return iCurrent;
  221. }