home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / afxmem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  10.0 KB  |  393 lines

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