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.

260 lines
9.1 KiB

  1. #ifndef _BUCKET_HPP_
  2. #define _BUCKET_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. #include "Connections.hpp"
  25. #include "Page.hpp"
  26. /********************************************************************/
  27. /* */
  28. /* Class forward references. */
  29. /* */
  30. /* We need to refer to the following classes before they are */
  31. /* fully specified so here we list them as forward references. */
  32. /* */
  33. /********************************************************************/
  34. class CACHE;
  35. class HEAP;
  36. /********************************************************************/
  37. /* */
  38. /* A collection of pages. */
  39. /* */
  40. /* A bucket is a collection of pages capable of allocating */
  41. /* fixed sized memory elements. The pages are allocated from */
  42. /* from larger buckets and are stored in a linked list in */
  43. /* order of ascending of page addresses. */
  44. /* */
  45. /********************************************************************/
  46. class BUCKET : public CONNECTIONS
  47. {
  48. //
  49. // Private type definitions.
  50. //
  51. // All allocations are registered in bit vectors.
  52. // Here we have some prototypes for seriously
  53. // optimized functions to do address to bit
  54. // vector computations.
  55. //
  56. typedef VOID *(BUCKET::*COMPUTE_ADDRESS)
  57. (
  58. CHAR *Address,
  59. SBIT32 Offset
  60. );
  61. typedef SBIT32 (BUCKET::*COMPUTE_OFFSET)
  62. (
  63. SBIT32 Displacement,
  64. BOOLEAN *Found
  65. );
  66. //
  67. // Private data.
  68. //
  69. // A bucket owns all the memory of a given size
  70. // and manages it. Above it is a cache to
  71. // protect it from huge number of calls and
  72. // below it are the connections to various
  73. // other classes. The 'AllocationSize' is the
  74. // buckets allocation size. The 'ChunkSize' is
  75. // chunking size which is typically half way
  76. // between the 'AllocationSize' and the 'PageSize'.
  77. // The 'PageSize' is the size of the bucket
  78. // where this bucket gets its space.
  79. //
  80. //
  81. SBIT32 AllocationSize;
  82. SBIT32 ChunkSize;
  83. SBIT32 PageSize;
  84. //
  85. // It is the job of the bucket to keep track of
  86. // all the information relating to allocations
  87. // of a given 'AllocationSize'. The 'ActivePages'
  88. // keeps track of the number of available pages
  89. // in the 'BucketList'. The 'BucketList' is a
  90. // linked list of pages that have available space.
  91. // The 'CurrentPage' contains the highest address
  92. // of the first page in the 'BucketList'.
  93. //
  94. SBIT32 ActivePages;
  95. LIST BucketList;
  96. VOID *CurrentPage;
  97. //
  98. // A bucket needs to be able to quickly convert
  99. // bit vector offsets to addresses (and vice versa).
  100. // The 'AllocationShift' is set when the
  101. // 'AllocationSize' is a power of two to avoid
  102. // any divides. The 'ChunkShift' is set when the
  103. // 'ChunckSize' is a power of two to avoid some
  104. // divides. The 'ComputeAddressFunction' and
  105. // 'ComputeOffsetFunction' point to optimized
  106. // functions to do conversions that are selected
  107. // by the constructor.
  108. //
  109. SBIT32 AllocationShift;
  110. SBIT32 ChunkShift;
  111. COMPUTE_ADDRESS ComputeAddressFunction;
  112. COMPUTE_OFFSET ComputeOffsetFunction;
  113. //
  114. // A bucket typically contains a collection of
  115. // pages. As all pages are the same data that
  116. // should really be stored in page descriptions
  117. // is instead stored in the bucket to save space.
  118. // The 'NumberOfElements' contains the number of
  119. // elements in each pages bit vector. The
  120. // 'SizeOfChunks' contains the pre-computed chunk
  121. // size. The 'SizeOfElements' contains the number
  122. // of words in the pages bit vector. The 'SizeKey'
  123. // contains an index which selects the size of the
  124. // bit vector when a new page is created.
  125. //
  126. SBIT16 NumberOfElements;
  127. SBIT16 SizeOfChunks;
  128. SBIT16 SizeOfElements;
  129. SBIT16 SizeKey;
  130. public:
  131. //
  132. // Public functions.
  133. //
  134. // The functionality provided by this class pretty
  135. // much matches the external API. Nonetheless, these
  136. // APIs are protected from excessive calls by a fast
  137. // cache that is derived from this class.
  138. //
  139. BUCKET
  140. (
  141. SBIT32 NewAllocationSize,
  142. SBIT32 NewChunkSize,
  143. SBIT32 NewPageSize
  144. );
  145. BOOLEAN Delete( VOID *Address,PAGE *Page,SBIT32 Version );
  146. VOID DeleteFromBucketList( PAGE *Page );
  147. VOID InsertInBucketList( PAGE *Page );
  148. BOOLEAN MultipleDelete
  149. (
  150. ADDRESS_AND_PAGE *Array,
  151. SBIT32 *Deleted,
  152. SBIT32 Size
  153. );
  154. BOOLEAN MultipleNew
  155. (
  156. SBIT32 *Actual,
  157. VOID *Array[],
  158. SBIT32 Requested
  159. );
  160. VOID *New( BOOLEAN SubDivided,SBIT32 NewSize = NoSize );
  161. VOID ReleaseSpace( SBIT32 MaxActivePages );
  162. VOID UpdateBucket
  163. (
  164. FIND *NewFind,
  165. HEAP *NewHeap,
  166. NEW_PAGE *NewPages,
  167. CACHE *NewParentCache
  168. );
  169. ~BUCKET( VOID );
  170. //
  171. // Public inline functions.
  172. //
  173. // It saves a significant amount of space by putting
  174. // common information in the bucket instead of a
  175. // separate copy in each page description. Nonetheless,
  176. // it means that both classes are very much dependent
  177. // upon each other.
  178. //
  179. INLINE VOID *ComputeAddress( CHAR *Address,SBIT32 Offset )
  180. { return (this ->* ComputeAddressFunction)( Address,Offset ); }
  181. INLINE SBIT32 ComputeOffset( SBIT32 Displacement,BOOLEAN *Found )
  182. { return (this ->* ComputeOffsetFunction)( Displacement,Found ); }
  183. INLINE SBIT32 GetAllocationSize( VOID )
  184. { return AllocationSize; }
  185. INLINE SBIT32 GetChunkSize( VOID )
  186. { return ChunkSize; }
  187. VOID *GetCurrentPage( VOID )
  188. { return CurrentPage; }
  189. INLINE SBIT16 GetNumberOfElements( VOID )
  190. { return NumberOfElements; }
  191. INLINE SBIT32 GetPageSize( VOID )
  192. { return PageSize; }
  193. INLINE SBIT16 GetSizeOfChunks( VOID )
  194. { return SizeOfChunks; }
  195. INLINE SBIT16 GetSizeOfElements( VOID )
  196. { return SizeOfElements; }
  197. INLINE SBIT16 GetSizeKey( VOID )
  198. { return SizeKey; }
  199. private:
  200. //
  201. // Private functions.
  202. //
  203. // When we need to convert an address to a bit
  204. // offset (or vice versa) we use one of the following
  205. // functions.
  206. //
  207. VOID *ComputeAddressBestCase( CHAR *Address,SBIT32 Offset );
  208. VOID *ComputeAddressGoodCase( CHAR *Address,SBIT32 Offset );
  209. VOID *ComputeAddressPoorCase( CHAR *Address,SBIT32 Offset );
  210. VOID *ComputeAddressWorstCase( CHAR *Address,SBIT32 Offset );
  211. SBIT32 ComputeOffsetBestCase( SBIT32 Displacement,BOOLEAN *Found );
  212. SBIT32 ComputeOffsetGoodCase( SBIT32 Displacement,BOOLEAN *Found );
  213. SBIT32 ComputeOffsetPoorCase( SBIT32 Displacement,BOOLEAN *Found );
  214. SBIT32 ComputeOffsetWorstCase( SBIT32 Displacement,BOOLEAN *Found );
  215. //
  216. // Disabled operations.
  217. //
  218. // All copy constructors and class assignment
  219. // operations are disabled.
  220. //
  221. BUCKET( CONST BUCKET & Copy );
  222. VOID operator=( CONST BUCKET & Copy );
  223. };
  224. #endif