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.

186 lines
4.6 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "tier0/platform.h"
  7. #include "tier0/icommandline.h"
  8. #include "tier0/dbg.h"
  9. #include "mem_helpers.h"
  10. #include <string.h>
  11. //#include <malloc.h>
  12. // NOTE: This has to be the last file included!
  13. #include "tier0/memdbgon.h"
  14. // Needed for debugging
  15. const char *g_pszModule = "tier0";
  16. bool g_bInitMemory = true;
  17. #if defined(PLATFORM_POSIX) || defined( PLATFORM_PS3)
  18. void DoApplyMemoryInitializations( void *pMem, size_t nSize )
  19. {
  20. }
  21. size_t CalcHeapUsed()
  22. {
  23. return 0;
  24. }
  25. #else
  26. unsigned long g_dwFeeFee = 0xffeeffee;
  27. // Generated by Mathematica.
  28. unsigned char g_RandomValues[256] = {
  29. 95, 126, 220, 71, 92, 179, 95, 219, 111, 150, 38, 155, 181, 62, 40, 231, 238,
  30. 54, 47, 55, 186, 204, 64, 70, 118, 94, 107, 251, 199, 140, 67, 87, 86, 127,
  31. 210, 41, 21, 90, 208, 24, 167, 204, 32, 254, 38, 51, 9, 11, 38, 33, 188, 104,
  32. 0, 75, 119, 24, 122, 203, 24, 164, 250, 224, 241, 182, 213, 201, 173, 67,
  33. 200, 255, 244, 227, 46, 219, 26, 149, 218, 132, 120, 154, 227, 244, 106, 198,
  34. 109, 87, 150, 40, 16, 99, 169, 193, 100, 156, 78, 171, 246, 47, 84, 119, 10,
  35. 52, 207, 171, 230, 90, 90, 127, 180, 153, 68, 140, 62, 14, 87, 57, 208, 154,
  36. 116, 29, 131, 177, 224, 187, 51, 148, 142, 245, 152, 230, 184, 117, 91, 146,
  37. 235, 153, 35, 104, 187, 177, 215, 131, 17, 49, 211, 244, 60, 152, 103, 248,
  38. 51, 224, 237, 240, 51, 30, 10, 233, 253, 106, 252, 73, 134, 136, 178, 86,
  39. 228, 107, 77, 255, 85, 242, 204, 119, 102, 53, 209, 35, 123, 32, 252, 210,
  40. 43, 12, 136, 167, 155, 210, 71, 254, 178, 172, 3, 230, 93, 208, 196, 68, 235,
  41. 16, 106, 189, 201, 177, 85, 78, 206, 187, 48, 68, 64, 190, 117, 236, 49, 174,
  42. 105, 63, 207, 70, 170, 93, 6, 110, 52, 111, 169, 92, 247, 86, 10, 174, 207,
  43. 240, 104, 209, 81, 177, 123, 189, 175, 212, 101, 219, 114, 243, 44, 91, 51,
  44. 139, 91, 57, 120, 41, 98, 119 };
  45. unsigned long g_iCurRandomValueOffset = 0;
  46. void InitializeToFeeFee( void *pMem, size_t nSize )
  47. {
  48. unsigned long *pCurDWord = (unsigned long*)pMem;
  49. size_t nDWords = nSize >> 2;
  50. while ( nDWords )
  51. {
  52. *pCurDWord = 0xffeeffee;
  53. ++pCurDWord;
  54. --nDWords;
  55. }
  56. unsigned char *pCurChar = (unsigned char*)pCurDWord;
  57. size_t nBytes = nSize & 3;
  58. size_t iOffset = 0;
  59. while ( nBytes )
  60. {
  61. *pCurChar = ((unsigned char*)&g_dwFeeFee)[iOffset];
  62. ++iOffset;
  63. --nBytes;
  64. ++pCurChar;
  65. }
  66. }
  67. void InitializeToRandom( void *pMem, size_t nSize )
  68. {
  69. unsigned char *pOut = (unsigned char *)pMem;
  70. for ( size_t i=0; i < nSize; i++ )
  71. {
  72. pOut[i] = g_RandomValues[(g_iCurRandomValueOffset & 255)];
  73. ++g_iCurRandomValueOffset;
  74. }
  75. }
  76. void DoApplyMemoryInitializations( void *pMem, size_t nSize )
  77. {
  78. if ( !pMem )
  79. return;
  80. // If they passed -noinitmemory on the command line, don't do anything here.
  81. Assert( g_bInitMemory );
  82. // First time we get in here, remember all the settings.
  83. static bool bDebuggerPresent = Plat_IsInDebugSession();
  84. static bool bCheckedCommandLine = false;
  85. static bool bRandomizeMemory = false;
  86. if ( !bCheckedCommandLine )
  87. {
  88. bCheckedCommandLine = true;
  89. //APS
  90. char *pStr = (char*)Plat_GetCommandLineA();
  91. if ( pStr )
  92. {
  93. char tempStr[512];
  94. strncpy( tempStr, pStr, sizeof( tempStr ) - 1 );
  95. tempStr[ sizeof( tempStr ) - 1 ] = 0;
  96. _strupr( tempStr );
  97. if ( strstr( tempStr, "-RANDOMIZEMEMORY" ) )
  98. bRandomizeMemory = true;
  99. if ( strstr( tempStr, "-NOINITMEMORY" ) )
  100. g_bInitMemory = false;
  101. }
  102. }
  103. if ( bRandomizeMemory )
  104. {
  105. // They asked for it.. randomize all the memory.
  106. InitializeToRandom( pMem, nSize );
  107. }
  108. else
  109. {
  110. if ( bDebuggerPresent )
  111. {
  112. // Ok, it's already set to 0xbaadf00d, but we want something that will make floating-point #'s NANs.
  113. InitializeToFeeFee( pMem, nSize );
  114. }
  115. else
  116. {
  117. #if defined(_DEBUG) || defined(USE_LIGHT_MEM_DEBUG)
  118. #if !defined(_DEBUG) && defined(LIGHT_MEM_DEBUG_REQUIRES_CMD_LINE_SWITCH)
  119. extern bool g_bUsingLMD;
  120. if ( !g_bUsingLMD )
  121. {
  122. return;
  123. }
  124. #endif
  125. // Ok, it's already set to 0xcdcdcdcd, but we want something that will make floating-point #'s NANs.
  126. InitializeToFeeFee( pMem, nSize );
  127. #endif
  128. }
  129. }
  130. }
  131. size_t CalcHeapUsed()
  132. {
  133. #if defined( _X360 )
  134. return 0;
  135. #else
  136. _HEAPINFO hinfo;
  137. int heapstatus;
  138. intp nTotal;
  139. nTotal = 0;
  140. hinfo._pentry = NULL;
  141. while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK )
  142. {
  143. nTotal += (hinfo._useflag == _USEDENTRY) ? hinfo._size : 0;
  144. }
  145. switch (heapstatus)
  146. {
  147. case _HEAPEMPTY:
  148. case _HEAPEND:
  149. // success
  150. break;
  151. default:
  152. // heap corrupted
  153. nTotal = -1;
  154. }
  155. return nTotal;
  156. #endif
  157. }
  158. #endif // not PLATFORM_POSIX