Counter Strike : Global Offensive Source Code
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.

456 lines
11 KiB

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