Team Fortress 2 Source Code as on 22/4/2020
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.

172 lines
4.1 KiB

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