summaryrefslogtreecommitdiff
path: root/external/vpc/tier0/mem_helpers.cpp
blob: 52f20034b31a6f0dccc0bc99d0c78fabf24dadbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//===========================================================================//

#include "tier0/platform.h"
#include "tier0/icommandline.h"
#include "tier0/dbg.h"
#include "mem_helpers.h"
#include <string.h>
//#include <malloc.h>

// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"

// Needed for debugging
const char *g_pszModule = "tier0";
bool g_bInitMemory = true;

#if defined(PLATFORM_POSIX) || defined( PLATFORM_PS3)
void DoApplyMemoryInitializations( void *pMem, size_t nSize )
{
}

size_t CalcHeapUsed()
{
	return 0;
}
#else

unsigned long g_dwFeeFee = 0xffeeffee;

// Generated by Mathematica.
unsigned char g_RandomValues[256] = {
	95, 126, 220, 71, 92, 179, 95, 219, 111, 150, 38, 155, 181, 62, 40, 231, 238,
	54, 47, 55, 186, 204, 64, 70, 118, 94, 107, 251, 199, 140, 67, 87, 86, 127,
	210, 41, 21, 90, 208, 24, 167, 204, 32, 254, 38, 51, 9, 11, 38, 33, 188, 104,
	0, 75, 119, 24, 122, 203, 24, 164, 250, 224, 241, 182, 213, 201, 173, 67,
	200, 255, 244, 227, 46, 219, 26, 149, 218, 132, 120, 154, 227, 244, 106, 198,
	109, 87, 150, 40, 16, 99, 169, 193, 100, 156, 78, 171, 246, 47, 84, 119, 10,
	52, 207, 171, 230, 90, 90, 127, 180, 153, 68, 140, 62, 14, 87, 57, 208, 154,
	116, 29, 131, 177, 224, 187, 51, 148, 142, 245, 152, 230, 184, 117, 91, 146,
	235, 153, 35, 104, 187, 177, 215, 131, 17, 49, 211, 244, 60, 152, 103, 248,
	51, 224, 237, 240, 51, 30, 10, 233, 253, 106, 252, 73, 134, 136, 178, 86,
	228, 107, 77, 255, 85, 242, 204, 119, 102, 53, 209, 35, 123, 32, 252, 210,
	43, 12, 136, 167, 155, 210, 71, 254, 178, 172, 3, 230, 93, 208, 196, 68, 235,
	16, 106, 189, 201, 177, 85, 78, 206, 187, 48, 68, 64, 190, 117, 236, 49, 174,
	105, 63, 207, 70, 170, 93, 6, 110, 52, 111, 169, 92, 247, 86, 10, 174, 207,
	240, 104, 209, 81, 177, 123, 189, 175, 212, 101, 219, 114, 243, 44, 91, 51,
	139, 91, 57, 120, 41, 98, 119 };

unsigned long g_iCurRandomValueOffset = 0;


void InitializeToFeeFee( void *pMem, size_t nSize )
{
	unsigned long *pCurDWord = (unsigned long*)pMem;
	size_t nDWords = nSize >> 2;
	while ( nDWords )
	{
		*pCurDWord = 0xffeeffee;
		++pCurDWord;
		--nDWords;
	}
	
	unsigned char *pCurChar = (unsigned char*)pCurDWord;
	size_t nBytes = nSize & 3;
	size_t iOffset = 0;
	while ( nBytes )
	{
		*pCurChar = ((unsigned char*)&g_dwFeeFee)[iOffset];
		++iOffset;
		--nBytes;
		++pCurChar;
	}			
}


void InitializeToRandom( void *pMem, size_t nSize )
{
	unsigned char *pOut = (unsigned char *)pMem;
	for ( size_t i=0; i < nSize; i++ )
	{
		pOut[i] = g_RandomValues[(g_iCurRandomValueOffset & 255)];
		++g_iCurRandomValueOffset;
	}
}


void DoApplyMemoryInitializations( void *pMem, size_t nSize )
{
	if ( !pMem )
		return;
	
	// If they passed -noinitmemory on the command line, don't do anything here.
	Assert( g_bInitMemory );

	// First time we get in here, remember all the settings.
	static bool bDebuggerPresent = Plat_IsInDebugSession();
	static bool bCheckedCommandLine = false;
	static bool bRandomizeMemory = false;
	if ( !bCheckedCommandLine )
	{
		bCheckedCommandLine = true;
		
		//APS
		char *pStr = (char*)Plat_GetCommandLineA();
		if ( pStr )
		{
			char tempStr[512];
			strncpy( tempStr, pStr, sizeof( tempStr ) - 1 );
			tempStr[ sizeof( tempStr ) - 1 ] = 0;
			_strupr( tempStr );
			
			if ( strstr( tempStr, "-RANDOMIZEMEMORY" ) )
				bRandomizeMemory = true;
			
			if ( strstr( tempStr, "-NOINITMEMORY" ) )
				g_bInitMemory = false;
		}
	}

	if ( bRandomizeMemory )
	{
		// They asked for it.. randomize all the memory.
		InitializeToRandom( pMem, nSize );
	}
	else
	{
		if ( bDebuggerPresent )
		{
			// Ok, it's already set to 0xbaadf00d, but we want something that will make floating-point #'s NANs.
			InitializeToFeeFee( pMem, nSize );
		}
		else
		{
#if defined(_DEBUG) || defined(USE_LIGHT_MEM_DEBUG)
#ifdef LIGHT_MEM_DEBUG_REQUIRES_CMD_LINE_SWITCH
			extern bool g_bUsingLMD;
			if ( !g_bUsingLMD )
			{
				return;
			}
#endif
			// Ok, it's already set to 0xcdcdcdcd, but we want something that will make floating-point #'s NANs.
			InitializeToFeeFee( pMem, nSize );
#endif
		}
	}
}

size_t CalcHeapUsed()
{
#if defined( _X360 )
	return 0;
#else
	_HEAPINFO	hinfo;
	int			heapstatus;
	intp			nTotal;

	nTotal = 0;
	hinfo._pentry = NULL;
	while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK )
	{
		nTotal += (hinfo._useflag == _USEDENTRY) ? hinfo._size : 0;
	}

	switch (heapstatus)
	{
		case _HEAPEMPTY:
		case _HEAPEND:
			// success
			break;

		default:
			// heap corrupted
			nTotal = -1;
	}

	return nTotal;
#endif
}

#endif // not PLATFORM_POSIX