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.

184 lines
7.0 KiB

  1. #ifndef _DELAY_HPP_
  2. #define _DELAY_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 "Lock.hpp"
  25. #include "Vector.hpp"
  26. /********************************************************************/
  27. /* */
  28. /* Constants exported from the class. */
  29. /* */
  30. /* The delay constants specify the initial size of the array */
  31. /* containing the list of allocations to be deleted. */
  32. /* */
  33. /********************************************************************/
  34. CONST SBIT32 BlockSize = 128;
  35. /********************************************************************/
  36. /* */
  37. /* Delayed memory deletion. */
  38. /* */
  39. /* This class provides general purpose memory delayed memory */
  40. /* deletion mechanism. */
  41. /* */
  42. /********************************************************************/
  43. template <class TYPE,class LOCK=NO_LOCK> class DELAY : public LOCK
  44. {
  45. //
  46. // Private data.
  47. //
  48. SBIT32 MaxSize;
  49. SBIT32 Used;
  50. TYPE **Block;
  51. public:
  52. //
  53. // Public functions.
  54. //
  55. DELAY( SBIT32 NewMaxSize = BlockSize );
  56. VOID DeferedDelete( TYPE *Memory );
  57. ~DELAY( VOID );
  58. //
  59. // Public inline functions.
  60. //
  61. INLINE CONST TYPE **AllocationList( VOID )
  62. { return ((CONST TYPE**) Block); };
  63. INLINE SBIT32 SizeOfBlock( VOID )
  64. { return Used; }
  65. private:
  66. //
  67. // Disabled operations.
  68. //
  69. DELAY( CONST DELAY & Copy );
  70. VOID operator=( CONST DELAY & Copy );
  71. };
  72. /********************************************************************/
  73. /* */
  74. /* Class constructor. */
  75. /* */
  76. /* Create a new block and prepare it for use. This call is */
  77. /* not thread safe and should only be made in a single thread */
  78. /* environment. */
  79. /* */
  80. /********************************************************************/
  81. template <class TYPE,class LOCK> DELAY<TYPE,LOCK>::DELAY( SBIT32 NewMaxSize )
  82. {
  83. #ifdef DEBUGGING
  84. if ( NewMaxSize > 0 )
  85. {
  86. #endif
  87. MaxSize = NewMaxSize;
  88. Used = 0;
  89. Block = new TYPE* [ NewMaxSize ];
  90. #ifdef DEBUGGING
  91. }
  92. else
  93. { Failure( "Max size in constructor for DELAY" ); }
  94. #endif
  95. }
  96. /********************************************************************/
  97. /* */
  98. /* Defered delete for an allocated memory block. */
  99. /* */
  100. /* An allocated memory block is registered for deletion by */
  101. /* the class destructor. */
  102. /* */
  103. /********************************************************************/
  104. template <class TYPE,class LOCK> VOID DELAY<TYPE,LOCK>::DeferedDelete
  105. (
  106. TYPE *Memory
  107. )
  108. {
  109. //
  110. // Claim an exclusive lock (if enabled).
  111. //
  112. ClaimExclusiveLock();
  113. //
  114. // Make sure we have enough space to register this memory
  115. // block for later deletion. If not allocate more space
  116. // and copy the existing data into the enlarged space.
  117. //
  118. if ( Used >= MaxSize )
  119. {
  120. REGISTER SBIT32 Count;
  121. REGISTER TYPE **NewBlock = new TYPE* [ (MaxSize *= ExpandStore) ];
  122. for ( Count=0;Count < Used;Count ++ )
  123. { NewBlock[ Count ] = Block[ Count ]; }
  124. delete [] Block;
  125. Block = NewBlock;
  126. }
  127. //
  128. // Register the allocated memory block.
  129. //
  130. Block[ Used ++ ] = Memory;
  131. //
  132. // Release any lock claimed earlier.
  133. //
  134. ReleaseExclusiveLock();
  135. }
  136. /********************************************************************/
  137. /* */
  138. /* Class destructor. */
  139. /* */
  140. /* Destory an allocation. This call is not thread safe and */
  141. /* should only be made in a single thread environment. */
  142. /* */
  143. /********************************************************************/
  144. template <class TYPE,class LOCK> DELAY<TYPE,LOCK>::~DELAY( VOID )
  145. {
  146. REGISTER SBIT32 Count;
  147. //
  148. // Delete the allocated memory blocks.
  149. //
  150. for ( Count = (Used - 1);Count >= 0;Count -- )
  151. { delete Block[ Count ]; }
  152. delete [] Block;
  153. }
  154. #endif