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.

385 lines
7.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: segmru.hxx
  7. //
  8. // Contents: Most Recently Used segments tracker for bigtable.
  9. //
  10. // Classes: CMRUSegments
  11. //
  12. // Functions:
  13. //
  14. // History: 3-23-95 srikants Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Class: CLinearRange ()
  21. //
  22. // Purpose: An object that has a MIN and a MAX.
  23. //
  24. // History: 4-11-95 srikants Created
  25. //
  26. // Notes:
  27. //
  28. //----------------------------------------------------------------------------
  29. class CLinearRange
  30. {
  31. public:
  32. void AssertValid()
  33. {
  34. Win4Assert( _low <= _high );
  35. }
  36. CLinearRange( ULONG low=0, ULONG high=0 ) : _low(low), _high(high)
  37. {
  38. AssertValid();
  39. }
  40. void Set( ULONG low, ULONG high )
  41. {
  42. _low = low;
  43. _high = high;
  44. AssertValid();
  45. }
  46. void SetLow( ULONG low )
  47. {
  48. _low = low;
  49. AssertValid();
  50. }
  51. ULONG GetLow() const { return _low; }
  52. ULONG GetHigh() const { return _high; }
  53. void SetHigh( ULONG high )
  54. {
  55. _high = high;
  56. AssertValid();
  57. }
  58. BOOL Intersects( CLinearRange & tst )
  59. {
  60. if ( tst.GetHigh() < _low )
  61. {
  62. return FALSE;
  63. }
  64. else if ( tst.GetLow() > _high )
  65. {
  66. return FALSE;
  67. }
  68. else return TRUE;
  69. }
  70. BOOL InRange( ULONG val ) const
  71. {
  72. return val >= _low && val <= _high;
  73. }
  74. void MoveLow( ULONG delta )
  75. {
  76. _low += delta;
  77. AssertValid();
  78. }
  79. void MoveHigh( ULONG delta )
  80. {
  81. _high += delta;
  82. AssertValid();
  83. }
  84. ULONG GetRange() const { return (_high - _low) + 1; }
  85. private:
  86. ULONG _low;
  87. ULONG _high;
  88. };
  89. //+---------------------------------------------------------------------------
  90. //
  91. // Class: CPosNum
  92. //
  93. // Purpose: A number which is > 0 and not wrap on decrements
  94. //
  95. // History: 4-20-95 srikants Created
  96. //
  97. // Notes:
  98. //
  99. //----------------------------------------------------------------------------
  100. class CPosNum
  101. {
  102. public:
  103. CPosNum() : _num(0) {}
  104. ULONG Get() const { return _num; }
  105. void Increment() { _num++; }
  106. void Decrement()
  107. {
  108. if ( _num > 0 )
  109. {
  110. _num--;
  111. }
  112. }
  113. private:
  114. ULONG _num;
  115. };
  116. //+---------------------------------------------------------------------------
  117. //
  118. // Class: CClosedNum ()
  119. //
  120. // Purpose: A closed number that has a min, max and a current value.
  121. //
  122. // History: 4-11-95 srikants Created
  123. //
  124. // Notes:
  125. //
  126. //----------------------------------------------------------------------------
  127. class CClosedNum
  128. {
  129. public:
  130. CClosedNum( ULONG low=0, ULONG high=0, ULONG val=0 )
  131. : _range(low, high),
  132. _val(val)
  133. {
  134. Win4Assert( IsValid() );
  135. }
  136. CClosedNum( ULONG low, CPosNum & high )
  137. : _range(low, high.Get()), _val(0)
  138. {
  139. Win4Assert( IsValid() );
  140. }
  141. void Set( ULONG low, ULONG high, ULONG val )
  142. {
  143. _range.Set(low, high);
  144. _val = val;
  145. Win4Assert( IsValid() );
  146. }
  147. void Set( CLinearRange & range )
  148. {
  149. _range = range;
  150. Win4Assert( IsValid() );
  151. }
  152. void SetLow( ULONG low )
  153. {
  154. _range.SetLow(low);
  155. Win4Assert( IsValid() );
  156. }
  157. void SetHigh( ULONG high )
  158. {
  159. _range.SetHigh(high);
  160. Win4Assert( IsValid() );
  161. }
  162. ULONG GetHigh() const { return _range.GetHigh(); }
  163. ULONG GetLow() const { return _range.GetLow(); }
  164. CLinearRange & GetRange() { return _range; }
  165. BOOL IsInRange(ULONG val) const
  166. {
  167. return _range.InRange(val);
  168. }
  169. BOOL IsValid() const
  170. {
  171. return IsInRange(_val);
  172. }
  173. ULONG Get() const
  174. {
  175. return _val;
  176. }
  177. ULONG operator=(ULONG rhs)
  178. {
  179. if ( IsInRange(rhs) )
  180. {
  181. _val = rhs;
  182. }
  183. return _val;
  184. }
  185. ULONG operator=(CPosNum &num)
  186. {
  187. return operator=(num.Get());
  188. }
  189. void Increment()
  190. {
  191. Win4Assert( IsValid() );
  192. if ( _val < _range.GetHigh() )
  193. {
  194. _val++;
  195. }
  196. }
  197. void Decrement()
  198. {
  199. Win4Assert( IsValid() );
  200. if ( _val > _range.GetLow() )
  201. {
  202. _val--;
  203. }
  204. }
  205. ULONG operator += (ULONG incr)
  206. {
  207. Win4Assert( IsValid() );
  208. if ( _val + incr <= _range.GetHigh() )
  209. {
  210. _val += incr;
  211. }
  212. else
  213. {
  214. _val = _range.GetHigh();
  215. }
  216. return _val;
  217. }
  218. ULONG operator -= (ULONG dcr)
  219. {
  220. Win4Assert( IsValid() );
  221. if ( _val >= _range.GetLow() + dcr )
  222. {
  223. _val -= dcr;
  224. }
  225. else
  226. {
  227. _val = _range.GetLow();
  228. }
  229. return _val;
  230. }
  231. private:
  232. CLinearRange _range;
  233. ULONG _val;
  234. };
  235. class CTableSegment;
  236. //+---------------------------------------------------------------------------
  237. //
  238. // Class: CWidSegmentMap
  239. //
  240. // Purpose: An entry that maps a WORKID to a segment.
  241. //
  242. // History: 4-11-95 srikants Created
  243. //
  244. // Notes:
  245. //
  246. //----------------------------------------------------------------------------
  247. class CWidSegmentMap : public CDoubleLink
  248. {
  249. public:
  250. CWidSegmentMap( WORKID wid = widInvalid, CTableSegment * pSeg = 0 )
  251. : _wid(wid), _pSegment(pSeg)
  252. { _next = _prev = 0; }
  253. WORKID GetWorkId() const { return _wid; }
  254. CTableSegment * GetSegment() { return _pSegment; }
  255. void Set( WORKID wid, CTableSegment * pSegment )
  256. {
  257. _wid = wid;
  258. _pSegment = pSegment;
  259. }
  260. void Set( CTableSegment * pSegment )
  261. {
  262. _pSegment = pSegment;
  263. }
  264. private:
  265. WORKID _wid;
  266. CTableSegment * _pSegment;
  267. };
  268. //
  269. // A list and iterator for CWidSegmentMap entries.
  270. //
  271. typedef class TDoubleList<CWidSegmentMap> CWidSegMapList;
  272. typedef class TFwdListIter< CWidSegmentMap, CWidSegMapList> CFwdWidSegMapIter;
  273. //+---------------------------------------------------------------------------
  274. //
  275. // Class: CMRUSegments
  276. //
  277. // Purpose: A class that tracks the most recently used segments.
  278. //
  279. // History: 4-11-95 srikants Created
  280. //
  281. // Notes:
  282. //
  283. //----------------------------------------------------------------------------
  284. class CMRUSegments
  285. {
  286. public:
  287. CMRUSegments( unsigned nMaxEntries ) : _nMaxEntries(nMaxEntries)
  288. {
  289. }
  290. ~CMRUSegments();
  291. void AddReplace( WORKID wid, CTableSegment * pSeg );
  292. BOOL IsSegmentInUse( CTableSegment * pSeg );
  293. void Invalidate( const CTableSegment * const pSeg );
  294. CWidSegMapList & GetList()
  295. {
  296. return _list;
  297. }
  298. void Remove( WORKID wid );
  299. BOOL IsEmpty() const { return _list.IsEmpty(); }
  300. private:
  301. //
  302. // Maximum number of entries to keep in the MRU list.
  303. //
  304. unsigned _nMaxEntries;
  305. //
  306. // List of WORKID/Segment pairs that are recently used.
  307. //
  308. CWidSegMapList _list;
  309. };