afxmem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE1_SEG
  12. #pragma code_seg(AFX_CORE1_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Debug memory globals and implementation helpers
  20. #ifdef _DEBUG // most of this file is for debugging
  21. void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine);
  22. /////////////////////////////////////////////////////////////////////////////
  23. // test allocation routines
  24. void* PASCAL CObject::operator new(size_t nSize)
  25. {
  26. #ifdef _AFX_NO_DEBUG_CRT
  27. return ::operator new(nSize);
  28. #else
  29. return ::operator new(nSize, _CLIENT_BLOCK, NULL, 0);
  30. #endif // _AFX_NO_DEBUG_CRT
  31. }
  32. void PASCAL CObject::operator delete(void* p)
  33. {
  34. #ifdef _AFX_NO_DEBUG_CRT
  35. free(p);
  36. #else
  37. _free_dbg(p, _CLIENT_BLOCK);
  38. #endif
  39. }
  40. #if 0 //__BORLANDC__ was _MSC_VER >= 1200
  41. void PASCAL CObject::operator delete(void* p, void*)
  42. {
  43. #ifdef _AFX_NO_DEBUG_CRT
  44. free(p);
  45. #else
  46. _free_dbg(p, _CLIENT_BLOCK);
  47. #endif
  48. }
  49. #endif
  50. #ifndef _AFX_NO_DEBUG_CRT
  51. void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
  52. {
  53. return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
  54. }
  55. #if _MSC_VER >= 1200
  56. void AFX_CDECL operator delete(void* pData, LPCSTR /* lpszFileName */,
  57. int /* nLine */)
  58. {
  59. ::operator delete(pData);
  60. }
  61. #endif
  62. void* PASCAL
  63. CObject::operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
  64. {
  65. return ::operator new(nSize, _CLIENT_BLOCK, lpszFileName, nLine);
  66. }
  67. #if _MSC_VER >= 1200
  68. void PASCAL
  69. CObject::operator delete(void *pObject, LPCSTR /* lpszFileName */,
  70. int /* nLine */)
  71. {
  72. #ifdef _AFX_NO_DEBUG_CRT
  73. free(pObject);
  74. #else
  75. _free_dbg(pObject, _CLIENT_BLOCK);
  76. #endif
  77. }
  78. #endif
  79. void* AFXAPI AfxAllocMemoryDebug(size_t nSize, BOOL bIsObject, LPCSTR lpszFileName, int nLine)
  80. {
  81. return _malloc_dbg(nSize, bIsObject ? _CLIENT_BLOCK : _NORMAL_BLOCK,
  82. lpszFileName, nLine);
  83. }
  84. void AFXAPI AfxFreeMemoryDebug(void* pbData, BOOL bIsObject)
  85. {
  86. _free_dbg(pbData, bIsObject ? _CLIENT_BLOCK : _NORMAL_BLOCK);
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // allocation failure hook, tracking turn on
  90. BOOL AFXAPI _AfxDefaultAllocHook(size_t, BOOL, LONG)
  91. { return TRUE; }
  92. AFX_STATIC_DATA AFX_ALLOC_HOOK pfnAllocHook = _AfxDefaultAllocHook;
  93. AFX_STATIC_DATA _CRT_ALLOC_HOOK pfnCrtAllocHook = NULL;
  94. #if _MSC_VER >= 1200
  95. int __cdecl _AfxAllocHookProxy(int nAllocType, void * pvData, size_t nSize,
  96. int nBlockUse, long lRequest, const unsigned char * szFilename, int nLine)
  97. #else
  98. int __cdecl _AfxAllocHookProxy(int nAllocType, void * pvData, size_t nSize,
  99. int nBlockUse, long lRequest, const char * szFilename, int nLine)
  100. #endif
  101. {
  102. #if _MSC_VER >= 1200
  103. if (nAllocType != _HOOK_ALLOC)
  104. return (pfnCrtAllocHook)(nAllocType, pvData, nSize,
  105. nBlockUse, lRequest, (const unsigned char*) szFilename, nLine);
  106. if ((pfnAllocHook)(nSize, _BLOCK_TYPE(nBlockUse) == _CLIENT_BLOCK, lRequest))
  107. return (pfnCrtAllocHook)(nAllocType, pvData, nSize,
  108. nBlockUse, lRequest, (const unsigned char*) szFilename, nLine);
  109. #else
  110. if (nAllocType != _HOOK_ALLOC)
  111. return (pfnCrtAllocHook)(nAllocType, pvData, nSize,
  112. nBlockUse, lRequest, szFilename, nLine);
  113. if ((pfnAllocHook)(nSize, _BLOCK_TYPE(nBlockUse) == _CLIENT_BLOCK, lRequest))
  114. return (pfnCrtAllocHook)(nAllocType, pvData, nSize,
  115. nBlockUse, lRequest, szFilename, nLine);
  116. #endif
  117. return FALSE;
  118. }
  119. AFX_ALLOC_HOOK AFXAPI AfxSetAllocHook(AFX_ALLOC_HOOK pfnNewHook)
  120. {
  121. if (pfnCrtAllocHook == NULL)
  122. pfnCrtAllocHook = _CrtSetAllocHook(_AfxAllocHookProxy);
  123. AFX_ALLOC_HOOK pfnOldHook = pfnAllocHook;
  124. pfnAllocHook = pfnNewHook;
  125. return pfnOldHook;
  126. }
  127. // This can be set to TRUE to override all AfxEnableMemoryTracking calls,
  128. // allowing all allocations, even MFC internal allocations to be tracked.
  129. BOOL _afxMemoryLeakOverride = FALSE;
  130. BOOL AFXAPI AfxEnableMemoryTracking(BOOL bTrack)
  131. {
  132. if (_afxMemoryLeakOverride)
  133. return TRUE;
  134. int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  135. if (bTrack)
  136. _CrtSetDbgFlag(nOldState | _CRTDBG_ALLOC_MEM_DF);
  137. else
  138. _CrtSetDbgFlag(nOldState & ~_CRTDBG_ALLOC_MEM_DF);
  139. return nOldState & _CRTDBG_ALLOC_MEM_DF;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. // stop on a specific memory request
  143. // Obsolete API
  144. void AFXAPI AfxSetAllocStop(LONG lRequestNumber)
  145. {
  146. _CrtSetBreakAlloc(lRequestNumber);
  147. }
  148. BOOL AFXAPI AfxCheckMemory()
  149. // check all of memory (look for memory tromps)
  150. {
  151. return _CrtCheckMemory();
  152. }
  153. // -- true if block of exact size, allocated on the heap
  154. // -- set *plRequestNumber to request number (or 0)
  155. BOOL AFXAPI AfxIsMemoryBlock(const void* pData, UINT nBytes,
  156. LONG* plRequestNumber)
  157. {
  158. return _CrtIsMemoryBlock(pData, nBytes, plRequestNumber, NULL, NULL);
  159. }
  160. /////////////////////////////////////////////////////////////////////////////
  161. // CMemoryState
  162. CMemoryState::CMemoryState()
  163. {
  164. memset(this, 0, sizeof(*this));
  165. }
  166. void CMemoryState::UpdateData()
  167. {
  168. for(int i = 0; i < nBlockUseMax; i++)
  169. {
  170. m_lCounts[i] = m_memState.lCounts[i];
  171. m_lSizes[i] = m_memState.lSizes[i];
  172. }
  173. m_lHighWaterCount = m_memState.lHighWaterCount;
  174. m_lTotalCount = m_memState.lTotalCount;
  175. }
  176. // fills 'this' with the difference, returns TRUE if significant
  177. BOOL CMemoryState::Difference(const CMemoryState& oldState,
  178. const CMemoryState& newState)
  179. {
  180. int nResult = _CrtMemDifference(&m_memState, &oldState.m_memState, &newState.m_memState);
  181. UpdateData();
  182. return nResult != 0;
  183. }
  184. void CMemoryState::DumpStatistics() const
  185. {
  186. _CrtMemDumpStatistics(&m_memState);
  187. }
  188. // -- fill with current memory state
  189. void CMemoryState::Checkpoint()
  190. {
  191. _CrtMemCheckpoint(&m_memState);
  192. UpdateData();
  193. }
  194. // Dump objects created after this memory state was checkpointed
  195. // Will dump all objects if this memory state wasn't checkpointed
  196. // Dump all objects, report about non-objects also
  197. // List request number in {}
  198. void CMemoryState::DumpAllObjectsSince() const
  199. {
  200. _CrtMemDumpAllObjectsSince(&m_memState);
  201. }
  202. /////////////////////////////////////////////////////////////////////////////
  203. // Enumerate all objects allocated in the diagnostic memory heap
  204. struct _AFX_ENUM_CONTEXT
  205. {
  206. void (*m_pfn)(CObject*,void*);
  207. void* m_pContext;
  208. };
  209. AFX_STATIC void _AfxDoForAllObjectsProxy(void* pObject, void* pContext)
  210. {
  211. _AFX_ENUM_CONTEXT* p = (_AFX_ENUM_CONTEXT*)pContext;
  212. (*p->m_pfn)((CObject*)pObject, p->m_pContext);
  213. }
  214. void AFXAPI
  215. AfxDoForAllObjects(void (AFX_CDECL *pfn)(CObject*, void*), void* pContext)
  216. {
  217. _AFX_ENUM_CONTEXT context;
  218. context.m_pfn = pfn;
  219. context.m_pContext = pContext;
  220. _CrtDoForAllClientObjects(_AfxDoForAllObjectsProxy, &context);
  221. }
  222. /////////////////////////////////////////////////////////////////////////////
  223. // Automatic debug memory diagnostics
  224. BOOL AFXAPI AfxDumpMemoryLeaks()
  225. {
  226. return _CrtDumpMemoryLeaks();
  227. }
  228. #endif // _AFX_NO_DEBUG_CRT
  229. #endif // _DEBUG
  230. /////////////////////////////////////////////////////////////////////////////
  231. // Non-diagnostic memory routines
  232. int AFX_CDECL AfxNewHandler(size_t /* nSize */)
  233. {
  234. AfxThrowMemoryException();
  235. return 0;
  236. }
  237. #pragma warning(disable: 4273)
  238. #ifndef _AFXDLL
  239. AFX_COMDAT _PNH _afxNewHandler = &AfxNewHandler;
  240. #endif
  241. _PNH AFXAPI AfxGetNewHandler(void)
  242. {
  243. #ifdef _AFXDLL
  244. AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  245. return pState->m_pfnNewHandler;
  246. #else
  247. return _afxNewHandler;
  248. #endif
  249. }
  250. _PNH AFXAPI AfxSetNewHandler(_PNH pfnNewHandler)
  251. {
  252. #ifdef _AFXDLL
  253. AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  254. _PNH pfnOldHandler = pState->m_pfnNewHandler;
  255. pState->m_pfnNewHandler = pfnNewHandler;
  256. return pfnOldHandler;
  257. #else
  258. _PNH pfnOldHandler = _afxNewHandler;
  259. _afxNewHandler = pfnNewHandler;
  260. return pfnOldHandler;
  261. #endif
  262. }
  263. AFX_STATIC_DATA const _PNH _pfnUninitialized = (_PNH)-1;
  264. void* __cdecl operator new(size_t nSize)
  265. {
  266. void* pResult;
  267. #ifdef _AFXDLL
  268. _PNH pfnNewHandler = _pfnUninitialized;
  269. #endif
  270. for (;;)
  271. {
  272. #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
  273. pResult = _malloc_dbg(nSize, _NORMAL_BLOCK, NULL, 0);
  274. #else
  275. pResult = malloc(nSize);
  276. #endif
  277. if (pResult != NULL)
  278. return pResult;
  279. #ifdef _AFXDLL
  280. if (pfnNewHandler == _pfnUninitialized)
  281. {
  282. AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  283. pfnNewHandler = pState->m_pfnNewHandler;
  284. }
  285. if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
  286. break;
  287. #else
  288. if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
  289. break;
  290. #endif
  291. }
  292. return pResult;
  293. }
  294. void __cdecl operator delete(void* p)
  295. {
  296. #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
  297. _free_dbg(p, _NORMAL_BLOCK);
  298. #else
  299. free(p);
  300. #endif
  301. }
  302. #ifdef _DEBUG
  303. void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
  304. {
  305. #ifdef _AFX_NO_DEBUG_CRT
  306. UNUSED_ALWAYS(nType);
  307. UNUSED_ALWAYS(lpszFileName);
  308. UNUSED_ALWAYS(nLine);
  309. return ::operator new(nSize);
  310. #else
  311. void* pResult;
  312. #ifdef _AFXDLL
  313. _PNH pfnNewHandler = _pfnUninitialized;
  314. #endif
  315. for (;;)
  316. {
  317. pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);
  318. if (pResult != NULL)
  319. return pResult;
  320. #ifdef _AFXDLL
  321. if (pfnNewHandler == _pfnUninitialized)
  322. {
  323. AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  324. pfnNewHandler = pState->m_pfnNewHandler;
  325. }
  326. if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
  327. break;
  328. #else
  329. if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
  330. break;
  331. #endif
  332. }
  333. return pResult;
  334. #endif
  335. }
  336. #endif //_DEBUG
  337. /////////////////////////////////////////////////////////////////////////////