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.

248 lines
9.3 KiB

  1. #ifndef _NEW_PAGE_HPP_
  2. #define _NEW_PAGE_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 "Environment.hpp"
  25. #include "Common.hpp"
  26. #include "Find.hpp"
  27. #include "Page.hpp"
  28. #include "Rockall.hpp"
  29. #include "Spinlock.hpp"
  30. /********************************************************************/
  31. /* */
  32. /* Constants exported from the class. */
  33. /* */
  34. /* The constants supplied here relate to various failure */
  35. /* conditions or situations where information is unknown. */
  36. /* */
  37. /********************************************************************/
  38. CONST SBIT16 NoSizeKey = -1;
  39. /********************************************************************/
  40. /* */
  41. /* Class forward references. */
  42. /* */
  43. /* We need to refer to the following classes before they are */
  44. /* fully specified so here we list them as forward references. */
  45. /* */
  46. /********************************************************************/
  47. class CACHE;
  48. /********************************************************************/
  49. /* */
  50. /* Create and delete pages. */
  51. /* */
  52. /* We would normally expect a class to manage its own memory. */
  53. /* However, this is quite difficult for the 'PAGE' class as it */
  54. /* is also responsible for managing the memory for the memory */
  55. /* allocator. So here we remove a potentially nasty chore */
  56. /* and isolate it in a class of its own. */
  57. /* */
  58. /********************************************************************/
  59. class NEW_PAGE : public ENVIRONMENT, public COMMON
  60. {
  61. //
  62. // Private structures.
  63. //
  64. // All the pages descriptions created by this
  65. // class and managed by the memory allocator
  66. // are linked in three list. One of these lists
  67. // is managed by this class and is called the
  68. // 'NewPageList'. All pages are linked into
  69. // of three sub-lists. The 'ExternalList' is
  70. // a list of pages externally allocated pages.
  71. // The 'FullList' is a list of sub-allocated
  72. // space from the 'ExternalList' which is
  73. // partially or completely filled with alocations.
  74. // Finally, the 'FreeList' is a collection of
  75. // empty page descriptions all of the same size.
  76. //
  77. //
  78. typedef struct
  79. {
  80. SBIT32 Elements;
  81. LIST ExternalList;
  82. LIST FreeList;
  83. LIST FullList;
  84. SBIT32 Size;
  85. }
  86. NEW_PAGES;
  87. //
  88. // Private data.
  89. //
  90. // We manage a collection of data structures in
  91. // this class. The fundamental data structure
  92. // is a stack of externally allocated pages that
  93. // typically contain page descriptions that are
  94. // linked together into linked lists. The maximum
  95. // size of this stack is given by 'MaxStack'.
  96. // A few additional pages are consumed to allocate
  97. // stacks for caches in other classes.
  98. //
  99. SBIT32 MaxCacheStack;
  100. SBIT32 MaxNewPages;
  101. SBIT32 MaxStack;
  102. //
  103. // We keep track of various values to save having
  104. // to recompute them. The 'NaturalSize' is the
  105. // natural allocation size of our host (i.e. the OS).
  106. // The 'RootSize' is some multiple of the
  107. // 'NaturalSize' that this class uses to consume
  108. // memory. The 'ThreadSafe' flag indicates whether
  109. // we need to use locks. The "TopOfStack' is the
  110. // stack which contains pointers to the externally
  111. // allocated space. The 'Version' is the global
  112. // version number that is used to stamp each page
  113. // whenever it is allocated or deallocated. The
  114. // version number allows the code to ensure that
  115. // a page description has not been changed while
  116. // it was not holding the associated lock.
  117. //
  118. SBIT32 NaturalSize;
  119. SBIT32 RootCoreSize;
  120. SBIT32 RootStackSize;
  121. BOOLEAN ThreadSafe;
  122. SBIT32 TopOfStack;
  123. SBIT32 Version;
  124. //
  125. // We keep pointers to all the interesting data
  126. // structures we may need to update. The
  127. // 'CacheStack' points to block of memory that
  128. // is being sliced into small stacks for caches
  129. // in other classes. The 'NewPages' points to
  130. // an array of linked lists of page descriptions.
  131. // Each collection of page descriptions is
  132. // identical except for the size of the assocated
  133. // bit vector.
  134. //
  135. CHAR *CacheStack;
  136. NEW_PAGES *NewPages;
  137. VOID **Stack;
  138. //
  139. // We sometimes need to interact with some of
  140. // the other class. The 'Find" class is a hash
  141. // table of all the currently allocated pages.
  142. // The 'Rockall' class contains the external
  143. // API which includes the external memory memory
  144. // allocation functions. The 'TopCache' is the
  145. // largest cache we support and contains details
  146. // about top level allocations sizes.
  147. //
  148. FIND *Find;
  149. ROCKALL *Rockall;
  150. CACHE *TopCache;
  151. SPINLOCK Spinlock;
  152. public:
  153. //
  154. // Public functions.
  155. //
  156. // The public functions provide support for creating
  157. // new page descriptions and caches for other
  158. // classes. Although a lot of the fuinctionality
  159. // of the heap is masked from this class various
  160. // features such as deleting the entire heap
  161. // (i.e. 'DeleteAll') are still visable.
  162. //
  163. NEW_PAGE
  164. (
  165. FIND *NewFind,
  166. SBIT32 NewPageSizes[],
  167. ROCKALL *NewRockall,
  168. SBIT32 Size,
  169. BOOLEAN NewThreadSafe
  170. );
  171. PAGE *CreatePage( CACHE *Cache,SBIT32 NewSize = NoSize );
  172. VOID DeleteAll( BOOLEAN Recycle );
  173. VOID DeletePage( PAGE *Page );
  174. SBIT16 FindSizeKey( SBIT16 NumberOfElements );
  175. VOID *NewCacheStack( SBIT32 Size );
  176. VOID ResizeStack( VOID );
  177. BOOLEAN Walk( SEARCH_PAGE *Details );
  178. ~NEW_PAGE( VOID );
  179. //
  180. // Public inline functions.
  181. //
  182. // The public inline functions are typically either
  183. // small or highly performance sensitive. The
  184. // functions here mainly relate to locking and
  185. // updating various data structures.
  186. //
  187. INLINE VOID ClaimNewPageLock( VOID )
  188. {
  189. if ( (ThreadSafe) && (Find -> GetLockCount() == 0) )
  190. { Spinlock.ClaimLock(); }
  191. }
  192. INLINE VOID ReleaseNewPageLock( VOID )
  193. {
  194. if ( (ThreadSafe) && (Find -> GetLockCount() == 0) )
  195. { Spinlock.ReleaseLock(); }
  196. }
  197. INLINE VOID UpdateNewPage( CACHE *NewTopCache )
  198. { TopCache = NewTopCache; }
  199. private:
  200. //
  201. // Private functions.
  202. //
  203. // We support the overloading of the external
  204. // memory allocation routines. This is somewhat
  205. // unusual and means that we need to verify
  206. // that these functions do not supply us with
  207. // total rubbish.
  208. //
  209. VOID *VerifyNewArea( SBIT32 AlignMask,SBIT32 Size );
  210. //
  211. // Disabled operations.
  212. //
  213. // All copy constructors and class assignment
  214. // operations are disabled.
  215. //
  216. NEW_PAGE( CONST NEW_PAGE & Copy );
  217. VOID operator=( CONST NEW_PAGE & Copy );
  218. };
  219. #endif