Heap.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //---------------------------------------------------------------------------
  2. //
  3. // Heap.h -- This file contains the definition for the Base HEAP
  4. // Manager Class. The Base HEAP manager creates,
  5. // manages and destroys block of memory using Win32
  6. // virtual memory calls.
  7. //
  8. //---------------------------------------------------------------------------//
  9. // Copyright (C) Microsoft Corporation. All rights reserved. //
  10. //===========================================================================//
  11. #ifndef HEAP_H
  12. #define HEAP_H
  13. //---------------------------------------------------------------------------
  14. // Include Files
  15. #ifndef DSTD_H
  16. #include "dstd.h"
  17. #endif
  18. #ifndef DHEAP_H
  19. #include "dheap.h"
  20. #endif
  21. #include <memory.h>
  22. #include <gameos.hpp>
  23. //---------------------------------------------------------------------------
  24. // Macro Definitions
  25. #ifndef NO_ERR
  26. #define NO_ERR 0
  27. #endif
  28. #define OUT_OF_MEMORY 0xBADD0001
  29. #define ALLOC_ZERO 0xBADD0002
  30. #define ALLOC_OVERFLOW 0xBADD0003
  31. #define NULL_FREE_LIST 0xBADD0004
  32. #define HEAP_CORRUPTED 0xBADD0005
  33. #define BAD_FREED_PTR 0xBADD0006
  34. #define ALLOC_TOO_BIG 0xBADD0007
  35. #define COULDNT_COMMIT 0xBADD0008
  36. #define COULDNT_CREATE 0xBADD0009
  37. #ifdef _DEBUG
  38. #define allocatedBlockSize 12
  39. #else
  40. #define allocatedBlockSize 8
  41. #endif
  42. #define BASE_HEAP 0
  43. #define USER_HEAP 1
  44. #define MAX_HEAPS 256
  45. //---------------------------------------------------------------------------
  46. extern UserHeapPtr systemHeap;
  47. //---------------------------------------------------------------------------
  48. // Additional Debug Information
  49. #ifdef _DEBUG
  50. #define NUMMEMRECORDS 128000
  51. typedef struct _memRecord
  52. {
  53. void* ptr;
  54. unsigned long size;
  55. unsigned long stack[12];
  56. } memRecord;
  57. #endif
  58. typedef struct _GlobalHeapRec
  59. {
  60. HeapManagerPtr thisHeap;
  61. unsigned long heapSize;
  62. unsigned long totalCoreLeft;
  63. unsigned long coreLeft;
  64. } GlobalHeapRec;
  65. //---------------------------------------------------------------------------
  66. // Class Definitions
  67. class HeapManager
  68. {
  69. //Data Members
  70. //-------------
  71. protected:
  72. MemoryPtr heap;
  73. bool memReserved;
  74. unsigned long totalSize;
  75. unsigned long committedSize;
  76. unsigned long whoMadeMe;
  77. // BOOL VMQuery (PVOID pvAddress, PVMQUERY pVMQ);
  78. // LPCTSTR GetMemStorageText (DWORD dwStorage);
  79. // LPTSTR GetProtectText (DWORD dwProtect, LPTSTR szBuf, BOOL fShowFlags);
  80. // void ConstructRgnInfoLine (PVMQUERY pVMQ, LPTSTR szLine, int nMaxLen);
  81. // void ConstructBlkInfoLine (PVMQUERY pVMQ, LPTSTR szLine, int nMaxLen);
  82. public:
  83. HeapManagerPtr nxt;
  84. //Member Functions
  85. //-----------------
  86. public:
  87. void init (void);
  88. HeapManager (void);
  89. void destroy (void);
  90. ~HeapManager (void);
  91. long createHeap (unsigned long memSize);
  92. long commitHeap (unsigned long commitSize = 0);
  93. long decommitHeap (unsigned long decommitSize = 0);
  94. MemoryPtr getHeapPtr (void);
  95. operator MemoryPtr (void);
  96. void HeapManager::MemoryDump();
  97. virtual unsigned char heapType (void)
  98. {
  99. return BASE_HEAP;
  100. }
  101. unsigned long owner (void)
  102. {
  103. return whoMadeMe;
  104. }
  105. unsigned long tSize (void)
  106. {
  107. return committedSize;
  108. }
  109. };
  110. //---------------------------------------------------------------------------
  111. struct HeapBlock
  112. {
  113. unsigned long blockSize;
  114. HeapBlockPtr upperBlock;
  115. HeapBlockPtr previous;
  116. HeapBlockPtr next;
  117. };
  118. //---------------------------------------------------------------------------
  119. class UserHeap : public HeapManager
  120. {
  121. //Data Members
  122. //-------------
  123. protected:
  124. HeapBlockPtr heapStart;
  125. HeapBlockPtr heapEnd;
  126. HeapBlockPtr firstNearBlock;
  127. unsigned long heapSize;
  128. bool mallocFatals;
  129. long heapState;
  130. char *heapName;
  131. bool useGOSGuardPage;
  132. HGOSHEAP gosHeap;
  133. #ifdef _DEBUG
  134. memRecord *recordArray;
  135. long recordCount;
  136. bool logMallocs;
  137. #endif
  138. //Member Functions
  139. //-----------------
  140. protected:
  141. void relink (HeapBlockPtr newBlock);
  142. void unlink (HeapBlockPtr oldBlock);
  143. bool mergeWithLower (HeapBlockPtr block);
  144. public:
  145. UserHeap (void);
  146. long init (unsigned long memSize, char *heapId = NULL, bool useGOS = false);
  147. ~UserHeap (void);
  148. void destroy (void);
  149. unsigned long totalCoreLeft (void);
  150. unsigned long coreLeft (void);
  151. unsigned long size (void) { return heapSize;}
  152. void *Malloc (unsigned long memSize);
  153. long Free (void *memBlock);
  154. void *calloc (unsigned long memSize);
  155. void walkHeap (bool printIt = FALSE, bool skipAllocated = FALSE);
  156. long getLastError (void);
  157. bool heapReady (void)
  158. {
  159. return (heapSize != 0);
  160. }
  161. void setMallocFatals (bool fatalFlag)
  162. {
  163. mallocFatals = fatalFlag;
  164. }
  165. virtual unsigned char heapType (void)
  166. {
  167. return USER_HEAP;
  168. }
  169. char *getHeapName (void)
  170. {
  171. return heapName;
  172. }
  173. bool pointerOnHeap (void *ptr);
  174. #ifdef _DEBUG
  175. void startHeapMallocLog (void); //This function will start recoding each malloc and
  176. //free to insure that there are no leaks.
  177. void stopHeapMallocLog (void); //This can be used to suspend logging when mallocs
  178. //that are not desired to be logged are called.
  179. void dumpRecordLog (void);
  180. #else
  181. void startHeapMallocLog (void) //DOES NOTHING IN RELEASE/PROFILE BUILDS!
  182. {
  183. }
  184. void stopHeapMallocLog (void)
  185. {
  186. }
  187. void dumpRecordLog (void)
  188. {
  189. }
  190. #endif
  191. };
  192. //---------------------------------------------------------------------------
  193. // This class tracks each heap which is created to provide more accurate
  194. // memory map information about Honor Bound.
  195. // NOTE this class just records heaps. It does NO allocating of its own.
  196. class HeapList
  197. {
  198. //Data Members
  199. //-------------
  200. protected:
  201. static GlobalHeapRec heapRecords[MAX_HEAPS];
  202. static bool heapInstrumented;
  203. //Member Functions
  204. //----------------
  205. public:
  206. void init (void)
  207. {
  208. memset(heapRecords,0,sizeof(GlobalHeapRec)*MAX_HEAPS);
  209. heapInstrumented = false;
  210. }
  211. HeapList (void)
  212. {
  213. init();
  214. }
  215. void destroy (void)
  216. {
  217. init();
  218. }
  219. ~HeapList (void)
  220. {
  221. destroy();
  222. }
  223. void addHeap (HeapManagerPtr newHeap);
  224. void removeHeap (HeapManagerPtr oldHeap);
  225. void update (void); //Called every frame in Debug to monitor heaps!
  226. void dumpLog (void);
  227. static void initializeStatistics();
  228. };
  229. //---------------------------------------------------------------------------
  230. typedef HeapList *HeapListPtr;
  231. extern HeapListPtr globalHeapList;
  232. //---------------------------------------------------------------------------
  233. #endif
  234. //---------------------------------------------------------------------------
  235. //
  236. // Edit Log
  237. //
  238. //---------------------------------------------------------------------------