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.

281 lines
7.5 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This header, which must be the final include in a .cpp (or .h) file,
  4. // causes all crt methods to use debugging versions of the memory allocators.
  5. // NOTE: Use memdbgoff.h to disable memory debugging.
  6. //
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. // SPECIAL NOTE! This file must *not* use include guards; we need to be able
  10. // to include this potentially multiple times (since we can deactivate debugging
  11. // by including memdbgoff.h)
  12. #if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE) && !defined(__SPU__)
  13. // SPECIAL NOTE #2: This must be the final include in a .cpp or .h file!!!
  14. #if defined(_DEBUG) && !defined(USE_MEM_DEBUG) && !defined( _PS3 )
  15. #define USE_MEM_DEBUG 1
  16. #endif
  17. // If debug build or ndebug and not already included MS custom alloc files, or already included this file
  18. #if (defined(_DEBUG) || !defined(_INC_CRTDBG)) || defined(MEMDBGON_H)
  19. #include "tier0/basetypes.h"
  20. #include "tier0/valve_off.h"
  21. #ifdef COMPILER_MSVC
  22. #include <tchar.h>
  23. #else
  24. #include <wchar.h>
  25. #endif
  26. #include <string.h>
  27. #ifndef _PS3
  28. #include <malloc.h>
  29. #endif
  30. #include "tier0/valve_on.h"
  31. #include "commonmacros.h"
  32. #include "memalloc.h"
  33. #ifdef _WIN32
  34. #ifndef MEMALLOC_REGION
  35. #define MEMALLOC_REGION 0
  36. #endif
  37. #else
  38. #undef MEMALLOC_REGION
  39. #endif
  40. #if defined(USE_MEM_DEBUG)
  41. #if defined( POSIX ) || defined( _PS3 )
  42. #define _NORMAL_BLOCK 1
  43. #include "tier0/valve_off.h"
  44. #include <cstddef>
  45. #include <new>
  46. #include <sys/types.h>
  47. #if !defined( DID_THE_OPERATOR_NEW )
  48. #define DID_THE_OPERATOR_NEW
  49. // posix doesn't have a new of this form, so we impl our own
  50. void* operator new( size_t nSize, int blah, const char *pFileName, int nLine );
  51. void* operator new[]( size_t nSize, int blah, const char *pFileName, int nLine );
  52. #endif
  53. #else // defined(POSIX)
  54. // Include crtdbg.h and make sure _DEBUG is set to 1.
  55. #if !defined(_DEBUG)
  56. #define _DEBUG 1
  57. #include <crtdbg.h>
  58. #undef _DEBUG
  59. #else
  60. #include <crtdbg.h>
  61. #endif // !defined(_DEBUG)
  62. #endif // defined(POSIX)
  63. #endif
  64. #include "tier0/memdbgoff.h"
  65. // --------------------------------------------------------
  66. // Debug/non-debug agnostic elements
  67. #define MEM_OVERRIDE_ON 1
  68. #undef malloc
  69. #undef realloc
  70. #undef calloc
  71. #undef _expand
  72. #undef free
  73. #undef _msize
  74. #undef _aligned_malloc
  75. #undef _aligned_free
  76. #ifndef MEMDBGON_H
  77. inline void *MemAlloc_InlineCallocMemset( void *pMem, size_t nCount, size_t nElementSize)
  78. {
  79. memset(pMem, 0, nElementSize * nCount);
  80. return pMem;
  81. }
  82. #endif
  83. #define calloc(c, s) MemAlloc_InlineCallocMemset(malloc(c*s), c, s)
  84. #ifndef USE_LIGHT_MEM_DEBUG
  85. #define free(p) g_pMemAlloc->Free( p )
  86. #define _aligned_free( p ) MemAlloc_FreeAligned( p )
  87. #else
  88. extern const char *g_pszModule;
  89. #define free(p) g_pMemAlloc->Free( p, ::g_pszModule, 0 )
  90. #define _aligned_free( p ) MemAlloc_FreeAligned( p, ::g_pszModule, 0 )
  91. #endif
  92. #define _msize(p) g_pMemAlloc->GetSize( p )
  93. #define _expand(p, s) _expand_NoLongerSupported(p, s)
  94. // --------------------------------------------------------
  95. // Debug path
  96. #if defined(USE_MEM_DEBUG)
  97. #define malloc(s) MemAlloc_Alloc( s, __FILE__, __LINE__)
  98. #define realloc(p, s) g_pMemAlloc->Realloc( p, s, __FILE__, __LINE__ )
  99. #define _aligned_malloc( s, a ) MemAlloc_AllocAlignedFileLine( s, a, __FILE__, __LINE__ )
  100. #define _malloc_dbg(s, t, f, l) WHYCALLINGTHISDIRECTLY(s)
  101. #undef new
  102. #if defined( _PS3 )
  103. #ifndef PS3_OPERATOR_NEW_WRAPPER_DEFINED
  104. #define PS3_OPERATOR_NEW_WRAPPER_DEFINED
  105. inline void* operator new( size_t nSize, int blah, const char *pFileName, int nLine ) { return g_pMemAlloc->IndirectAlloc( nSize, pFileName, nLine ); }
  106. inline void* operator new[]( size_t nSize, int blah, const char *pFileName, int nLine ) { return g_pMemAlloc->IndirectAlloc( nSize, pFileName, nLine ); }
  107. #endif
  108. #define new new( 1, __FILE__, __LINE__ )
  109. #elif !defined( GNUC )
  110. #if defined(__AFX_H__) && defined(DEBUG_NEW)
  111. #define new DEBUG_NEW
  112. #else
  113. #define MEMALL_DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
  114. #define new MEMALL_DEBUG_NEW
  115. #endif
  116. #endif
  117. #undef _strdup
  118. #undef strdup
  119. #undef _wcsdup
  120. #undef wcsdup
  121. #define _strdup(s) MemAlloc_StrDup(s, __FILE__, __LINE__)
  122. #define strdup(s) MemAlloc_StrDup(s, __FILE__, __LINE__)
  123. #define _wcsdup(s) MemAlloc_WcStrDup(s, __FILE__, __LINE__)
  124. #define wcsdup(s) MemAlloc_WcStrDup(s, __FILE__, __LINE__)
  125. // Make sure we don't define strdup twice
  126. #if !defined(MEMDBGON_H)
  127. inline char *MemAlloc_StrDup(const char *pString, const char *pFileName, unsigned nLine)
  128. {
  129. char *pMemory;
  130. if (!pString)
  131. return NULL;
  132. size_t len = strlen(pString) + 1;
  133. if ((pMemory = (char *)MemAlloc_Alloc(len, pFileName, nLine)) != NULL)
  134. {
  135. return strcpy( pMemory, pString );
  136. }
  137. return NULL;
  138. }
  139. inline wchar_t *MemAlloc_WcStrDup(const wchar_t *pString, const char *pFileName, unsigned nLine)
  140. {
  141. wchar_t *pMemory;
  142. if (!pString)
  143. return NULL;
  144. size_t len = (wcslen(pString) + 1);
  145. if ((pMemory = (wchar_t *)MemAlloc_Alloc(len * sizeof(wchar_t), pFileName, nLine)) != NULL)
  146. {
  147. return wcscpy( pMemory, pString );
  148. }
  149. return NULL;
  150. }
  151. #endif // DBMEM_DEFINED_STRDUP
  152. #else
  153. // --------------------------------------------------------
  154. // Release path
  155. #ifndef USE_LIGHT_MEM_DEBUG
  156. #define malloc(s) MemAlloc_Alloc( s )
  157. #define realloc(p, s) g_pMemAlloc->Realloc( p, s )
  158. #define _aligned_malloc( s, a ) MemAlloc_AllocAligned( s, a )
  159. #else
  160. #define malloc(s) MemAlloc_Alloc( s, ::g_pszModule, 0 )
  161. #define realloc(p, s) g_pMemAlloc->Realloc( p, s, ::g_pszModule, 0 )
  162. #define _aligned_malloc( s, a ) MemAlloc_AllocAlignedFileLine( s, a, ::g_pszModule, 0 )
  163. #endif
  164. #ifndef _malloc_dbg
  165. #define _malloc_dbg(s, t, f, l) WHYCALLINGTHISDIRECTLY(s)
  166. #endif
  167. #undef new
  168. #if defined( _PS3 ) && !defined( _CERT )
  169. #ifndef PS3_OPERATOR_NEW_WRAPPER_DEFINED
  170. #define PS3_OPERATOR_NEW_WRAPPER_DEFINED
  171. inline void* operator new( size_t nSize, int blah, const char *pFileName, int nLine ) { return g_pMemAlloc->IndirectAlloc( nSize, pFileName, nLine ); }
  172. inline void* operator new[]( size_t nSize, int blah, const char *pFileName, int nLine ) { return g_pMemAlloc->IndirectAlloc( nSize, pFileName, nLine ); }
  173. #endif
  174. #define new new( 1, __FILE__, __LINE__ )
  175. #endif
  176. #undef _strdup
  177. #undef strdup
  178. #undef _wcsdup
  179. #undef wcsdup
  180. #define _strdup(s) MemAlloc_StrDup(s)
  181. #define strdup(s) MemAlloc_StrDup(s)
  182. #define _wcsdup(s) MemAlloc_WcStrDup(s)
  183. #define wcsdup(s) MemAlloc_WcStrDup(s)
  184. // Make sure we don't define strdup twice
  185. #if !defined(MEMDBGON_H)
  186. inline char *MemAlloc_StrDup(const char *pString)
  187. {
  188. char *pMemory;
  189. if (!pString)
  190. return NULL;
  191. size_t len = strlen(pString) + 1;
  192. if ((pMemory = (char *)malloc(len)) != NULL)
  193. {
  194. return strcpy( pMemory, pString );
  195. }
  196. return NULL;
  197. }
  198. inline wchar_t *MemAlloc_WcStrDup(const wchar_t *pString)
  199. {
  200. wchar_t *pMemory;
  201. if (!pString)
  202. return NULL;
  203. size_t len = (wcslen(pString) + 1);
  204. if ((pMemory = (wchar_t *)malloc(len * sizeof(wchar_t))) != NULL)
  205. {
  206. return wcscpy( pMemory, pString );
  207. }
  208. return NULL;
  209. }
  210. #endif // DBMEM_DEFINED_STRDUP
  211. #endif // USE_MEM_DEBUG
  212. #define MEMDBGON_H // Defined here so can be used above
  213. #else
  214. #if defined(USE_MEM_DEBUG)
  215. #ifndef _STATIC_LINKED
  216. #pragma message ("Note: file includes crtdbg.h directly, therefore will cannot use memdbgon.h in non-debug build")
  217. #else
  218. #error "Error: file includes crtdbg.h directly, therefore will cannot use memdbgon.h in non-debug build. Not recoverable in static build"
  219. #endif
  220. #endif
  221. #endif // _INC_CRTDBG
  222. #endif // !STEAM && !NO_MALLOC_OVERRIDE