Source code of Windows XP (NT5)
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.

209 lines
5.7 KiB

  1. #ifndef _DEBUG_HEAP_HPP_
  2. #define _DEBUG_HEAP_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 "Rockall.hpp"
  24. /********************************************************************/
  25. /* */
  26. /* The debug memory allocator. */
  27. /* */
  28. /* The debug memory allocator checks all the memory allocation */
  29. /* calls to make sure they are reasonable. If not then it */
  30. /* raises an execption at the point it detects a problem. */
  31. /* */
  32. /********************************************************************/
  33. class ROCKALL_LINKAGE DEBUG_HEAP : public ROCKALL
  34. {
  35. //
  36. // Private type definitions.
  37. //
  38. // A debug heap places a collection of guard words
  39. // before and after each allocation. It checks
  40. // these guard words everytime the allocation is
  41. // examined or modified.
  42. //
  43. typedef struct
  44. {
  45. int Size;
  46. int StartGuard;
  47. }
  48. DEBUG_HEADER;
  49. typedef struct
  50. {
  51. char MidGuard[ sizeof(int) ];
  52. int EndGuard[1];
  53. }
  54. DEBUG_TRAILER;
  55. typedef struct
  56. {
  57. DEBUG_HEADER DebugHeader;
  58. DEBUG_TRAILER DebugTrailer;
  59. }
  60. DEBUG_GUARD;
  61. public:
  62. //
  63. // Public functions.
  64. //
  65. // A heaps public interface consists of a number
  66. // of groups of related APIs.
  67. //
  68. DEBUG_HEAP
  69. (
  70. int MaxFreeSpace = 0,
  71. bool Recycle = false,
  72. bool SingleImage = false,
  73. bool ThreadSafe = true
  74. );
  75. //
  76. // Manipulate allocations.
  77. //
  78. // The first group of functions manipulate
  79. // single or small arrays of allocations.
  80. //
  81. virtual bool Delete
  82. (
  83. void *Address,
  84. int Size = NoSize
  85. );
  86. virtual bool Details
  87. (
  88. void *Address,
  89. int *Space = NULL
  90. );
  91. virtual bool MultipleDelete
  92. (
  93. int Actual,
  94. void *Array[],
  95. int Size = NoSize
  96. );
  97. virtual bool MultipleNew
  98. (
  99. int *Actual,
  100. void *Array[],
  101. int Requested,
  102. int Size,
  103. int *Space = NULL,
  104. bool Zero = false
  105. );
  106. virtual void *New
  107. (
  108. int Size,
  109. int *Space = NULL,
  110. bool Zero = false
  111. );
  112. virtual void *Resize
  113. (
  114. void *Address,
  115. int NewSize,
  116. int Move = 1,
  117. int *Space = NULL,
  118. bool NoDelete = false,
  119. bool Zero = false
  120. );
  121. virtual bool Verify
  122. (
  123. void *Address = NULL,
  124. int *Space = NULL
  125. );
  126. //
  127. // Manipulate the heap.
  128. //
  129. // The second group of functions act upon a heap
  130. // as a whole.
  131. //
  132. virtual void DeleteAll( bool Recycle = true );
  133. virtual void HeapLeaks( void );
  134. virtual void Truncate( int MaxFreeSpace = 0 );
  135. virtual bool Walk
  136. (
  137. bool *Active,
  138. void **Address,
  139. int *Space
  140. );
  141. //
  142. // Low level heap interface.
  143. //
  144. // The third group of functions are called by the
  145. // heap to aquire or release large blocks of memory.
  146. // These functions can be overloaded to enable the
  147. // heap work in constrained environments.
  148. //
  149. virtual void *NewArea( int AlignMask,int Size );
  150. virtual ~DEBUG_HEAP( void );
  151. private:
  152. //
  153. // Private functions.
  154. //
  155. // A debug heap verifies each allocation using a
  156. // collection of private functions.
  157. //
  158. DEBUG_HEADER *ComputeHeaderAddress( void *Address )
  159. {
  160. register int HeaderSize = sizeof(DEBUG_HEADER);
  161. return ((DEBUG_HEADER*) (((char*) Address) - HeaderSize));
  162. }
  163. void *ComputeDataAddress( DEBUG_HEADER *Header )
  164. { return ((void*) & Header[1]); }
  165. void ResetGuardWords( DEBUG_HEADER *Header,int TotalSize );
  166. void SetGuardWords( DEBUG_HEADER *Header,int Size,int TotalSize );
  167. void TestGuardWords( DEBUG_HEADER *Header,int TotalSize );
  168. void UnmodifiedGuardWords( DEBUG_HEADER *Header,int TotalSize );
  169. void UpdateGuardWords( DEBUG_HEADER *Header,int Size,int TotalSize );
  170. //
  171. // Disabled operations.
  172. //
  173. // All copy constructors and class assignment
  174. // operations are disabled.
  175. //
  176. DEBUG_HEAP( const DEBUG_HEAP & Copy );
  177. void operator=( const DEBUG_HEAP & Copy );
  178. };
  179. #endif