-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathplatform.cpp
More file actions
478 lines (405 loc) · 11 KB
/
platform.cpp
File metadata and controls
478 lines (405 loc) · 11 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//
#include "pch_tier0.h"
#include <time.h>
#if defined(_WIN32) && !defined(_X360)
#include <errno.h>
#endif
#include <assert.h>
#include "tier0/platform.h"
#include "tier0/minidump.h"
#ifdef _X360
#include "xbox/xbox_console.h"
#include "xbox/xbox_win32stubs.h"
#else
#include "tier0/vcrmode.h"
#endif
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
#include "tier0/memalloc.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#endif
//our global error callback function. Note that this is not initialized, but static space guarantees this is NULL at app start.
//If you initialize, it will set to zero again when the CPP runs its static initializers, which could stomp the value if another
//CPP sets this value while initializing its static space
static ExitProcessWithErrorCBFn g_pfnExitProcessWithErrorCB; //= NULL
#ifndef _X360
extern VCRMode_t g_VCRMode;
#endif
static LARGE_INTEGER g_PerformanceFrequency;
static double g_PerformanceCounterToS;
static double g_PerformanceCounterToMS;
static double g_PerformanceCounterToUS;
static LARGE_INTEGER g_ClockStart;
static bool s_bTimeInitted;
// Benchmark mode uses this heavy-handed method
static bool g_bBenchmarkMode = false;
static double g_FakeBenchmarkTime = 0;
static double g_FakeBenchmarkTimeInc = 1.0 / 66.0;
static void InitTime()
{
if( !s_bTimeInitted )
{
s_bTimeInitted = true;
QueryPerformanceFrequency(&g_PerformanceFrequency);
g_PerformanceCounterToS = 1.0 / g_PerformanceFrequency.QuadPart;
g_PerformanceCounterToMS = 1e3 / g_PerformanceFrequency.QuadPart;
g_PerformanceCounterToUS = 1e6 / g_PerformanceFrequency.QuadPart;
QueryPerformanceCounter(&g_ClockStart);
}
}
bool Plat_IsInBenchmarkMode()
{
return g_bBenchmarkMode;
}
void Plat_SetBenchmarkMode( bool bBenchmark )
{
g_bBenchmarkMode = bBenchmark;
}
double Plat_FloatTime()
{
if (! s_bTimeInitted )
InitTime();
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return g_FakeBenchmarkTime;
}
LARGE_INTEGER CurrentTime;
QueryPerformanceCounter( &CurrentTime );
double fRawSeconds = (double)( CurrentTime.QuadPart - g_ClockStart.QuadPart ) * g_PerformanceCounterToS;
return fRawSeconds;
}
uint32 Plat_MSTime()
{
if (! s_bTimeInitted )
InitTime();
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return (uint32)(g_FakeBenchmarkTime * 1000.0);
}
LARGE_INTEGER CurrentTime;
QueryPerformanceCounter( &CurrentTime );
return (uint32) ( ( CurrentTime.QuadPart - g_ClockStart.QuadPart ) * g_PerformanceCounterToMS );
}
uint64 Plat_USTime()
{
if (! s_bTimeInitted )
InitTime();
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return (uint64)(g_FakeBenchmarkTime * 1e6);
}
LARGE_INTEGER CurrentTime;
QueryPerformanceCounter( &CurrentTime );
return (uint64) ( ( CurrentTime.QuadPart - g_ClockStart.QuadPart ) * g_PerformanceCounterToUS );
}
void GetCurrentDate( int *pDay, int *pMonth, int *pYear )
{
struct tm *pNewTime;
time_t long_time;
time( &long_time ); /* Get time as long integer. */
pNewTime = localtime( &long_time ); /* Convert to local time. */
*pDay = pNewTime->tm_mday;
*pMonth = pNewTime->tm_mon + 1;
*pYear = pNewTime->tm_year + 1900;
}
// Wraps the thread-safe versions of ctime. buf must be at least 26 bytes
char *Plat_ctime( const time_t *timep, char *buf, size_t bufsize )
{
if ( EINVAL == ctime_s( buf, bufsize, timep ) )
return NULL;
else
return buf;
}
void Plat_GetModuleFilename( char *pOut, int nMaxBytes )
{
#ifdef PLATFORM_WINDOWS_PC
SetLastError( ERROR_SUCCESS ); // clear the error code
GetModuleFileName( NULL, pOut, nMaxBytes );
if ( GetLastError() != ERROR_SUCCESS )
Error( "Plat_GetModuleFilename: The buffer given is too small (%d bytes).", nMaxBytes );
#elif PLATFORM_X360
pOut[0] = 0x00; // return null string on Xbox 360
#else
// We shouldn't need this on POSIX.
Assert( false );
pOut[0] = 0x00; // Null the returned string in release builds
#endif
}
void Plat_ExitProcess( int nCode )
{
#if defined( _WIN32 ) && !defined( _X360 )
// We don't want global destructors in our process OR in any DLL to get executed.
// _exit() avoids calling global destructors in our module, but not in other DLLs.
const char *pchCmdLineA = Plat_GetCommandLineA();
if ( nCode || ( strstr( pchCmdLineA, "gc.exe" ) && strstr( pchCmdLineA, "gc.dll" ) && strstr( pchCmdLineA, "-gc" ) ) )
{
int *x = NULL; *x = 1; // cause a hard crash, GC is not allowed to exit voluntarily from gc.dll
}
TerminateProcess( GetCurrentProcess(), nCode );
#elif defined(_PS3)
// We do not use this path to exit on PS3 (naturally), rather we want a clear crash:
int *x = NULL; *x = 1;
#else
_exit( nCode );
#endif
}
void Plat_ExitProcessWithError( int nCode, bool bGenerateMinidump )
{
//try to delegate out if they have registered a callback
if( g_pfnExitProcessWithErrorCB )
{
if( g_pfnExitProcessWithErrorCB( nCode ) )
return;
}
//handle default behavior
if( bGenerateMinidump )
{
//don't generate mini dumps in the debugger
if( !Plat_IsInDebugSession() )
{
WriteMiniDump();
}
}
//and exit our process
Plat_ExitProcess( nCode );
}
void Plat_SetExitProcessWithErrorCB( ExitProcessWithErrorCBFn pfnCB )
{
g_pfnExitProcessWithErrorCB = pfnCB;
}
// Wraps the thread-safe versions of gmtime
struct tm *Plat_gmtime( const time_t *timep, struct tm *result )
{
if ( EINVAL == gmtime_s( result, timep ) )
return NULL;
else
return result;
}
time_t Plat_timegm( struct tm *timeptr )
{
return _mkgmtime( timeptr );
}
// Wraps the thread-safe versions of localtime
struct tm *Plat_localtime( const time_t *timep, struct tm *result )
{
if ( EINVAL == localtime_s( result, timep ) )
return NULL;
else
return result;
}
bool vtune( bool resume )
{
#ifndef _X360
static bool bInitialized = false;
static void (__cdecl *VTResume)(void) = NULL;
static void (__cdecl *VTPause) (void) = NULL;
// Grab the Pause and Resume function pointers from the VTune DLL the first time through:
if( !bInitialized )
{
bInitialized = true;
HINSTANCE pVTuneDLL = LoadLibrary( "vtuneapi.dll" );
if( pVTuneDLL )
{
VTResume = (void(__cdecl *)())GetProcAddress( pVTuneDLL, "VTResume" );
VTPause = (void(__cdecl *)())GetProcAddress( pVTuneDLL, "VTPause" );
}
}
// Call the appropriate function, as indicated by the argument:
if( resume && VTResume )
{
VTResume();
return true;
}
else if( !resume && VTPause )
{
VTPause();
return true;
}
#endif
return false;
}
bool Plat_IsInDebugSession()
{
#if defined( _WIN32 ) && !defined( _X360 )
return (IsDebuggerPresent() != 0);
#elif defined( _WIN32 ) && defined( _X360 )
return (XBX_IsDebuggerPresent() != 0);
#elif defined( LINUX )
#error This code is implemented in platform_posix.cpp
#else
return false;
#endif
}
void Plat_DebugString( const char * psz )
{
#if defined( _WIN32 ) && !defined( _X360 )
::OutputDebugStringA( psz );
#elif defined( _WIN32 ) && defined( _X360 )
XBX_OutputDebugString( psz );
#endif
}
const tchar *Plat_GetCommandLine()
{
#ifdef TCHAR_IS_WCHAR
return GetCommandLineW();
#else
return GetCommandLine();
#endif
}
bool GetMemoryInformation( MemoryInformation *pOutMemoryInfo )
{
if ( !pOutMemoryInfo )
return false;
MEMORYSTATUSEX memStat;
ZeroMemory( &memStat, sizeof( MEMORYSTATUSEX ) );
memStat.dwLength = sizeof( MEMORYSTATUSEX );
if ( !GlobalMemoryStatusEx( &memStat ) )
return false;
const uint cOneMb = 1024 * 1024;
switch ( pOutMemoryInfo->m_nStructVersion )
{
case 0:
( *pOutMemoryInfo ).m_nPhysicalRamMbTotal = memStat.ullTotalPhys / cOneMb;
( *pOutMemoryInfo ).m_nPhysicalRamMbAvailable = memStat.ullAvailPhys / cOneMb;
( *pOutMemoryInfo ).m_nVirtualRamMbTotal = memStat.ullTotalVirtual / cOneMb;
( *pOutMemoryInfo ).m_nVirtualRamMbAvailable = memStat.ullAvailVirtual / cOneMb;
break;
default:
return false;
};
return true;
}
const char *Plat_GetCommandLineA()
{
return GetCommandLineA();
}
//--------------------------------------------------------------------------------------------------
// Watchdog timer
//--------------------------------------------------------------------------------------------------
void Plat_BeginWatchdogTimer( int nSecs )
{
}
void Plat_EndWatchdogTimer( void )
{
}
int Plat_GetWatchdogTime( void )
{
return 0;
}
void Plat_SetWatchdogHandlerFunction( Plat_WatchDogHandlerFunction_t function )
{
}
bool Is64BitOS()
{
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
static LPFN_ISWOW64PROCESS pfnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle("kernel32"), "IsWow64Process" );
static BOOL bIs64bit = FALSE;
static bool bInitialized = false;
if ( bInitialized )
return bIs64bit == (BOOL)TRUE;
else
{
bInitialized = true;
return pfnIsWow64Process && pfnIsWow64Process(GetCurrentProcess(), &bIs64bit) && bIs64bit;
}
}
// -------------------------------------------------------------------------------------------------- //
// Memory stuff.
//
// DEPRECATED. Still here to support binary back compatability of tier0.dll
//
// -------------------------------------------------------------------------------------------------- //
#ifndef _X360
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
typedef void (*Plat_AllocErrorFn)( unsigned long size );
void Plat_DefaultAllocErrorFn( unsigned long size )
{
}
Plat_AllocErrorFn g_AllocError = Plat_DefaultAllocErrorFn;
#endif
#ifndef _X360
CRITICAL_SECTION g_AllocCS;
class CAllocCSInit
{
public:
CAllocCSInit()
{
InitializeCriticalSection( &g_AllocCS );
}
} g_AllocCSInit;
#endif
#ifndef _X360
PLATFORM_INTERFACE void* Plat_Alloc( unsigned long size )
{
EnterCriticalSection( &g_AllocCS );
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
void *pRet = g_pMemAlloc->Alloc( size );
#else
void *pRet = malloc( size );
#endif
LeaveCriticalSection( &g_AllocCS );
if ( pRet )
{
return pRet;
}
else
{
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
g_AllocError( size );
#endif
return 0;
}
}
#endif
#ifndef _X360
PLATFORM_INTERFACE void* Plat_Realloc( void *ptr, unsigned long size )
{
EnterCriticalSection( &g_AllocCS );
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
void *pRet = g_pMemAlloc->Realloc( ptr, size );
#else
void *pRet = realloc( ptr, size );
#endif
LeaveCriticalSection( &g_AllocCS );
if ( pRet )
{
return pRet;
}
else
{
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
g_AllocError( size );
#endif
return 0;
}
}
#endif
#ifndef _X360
PLATFORM_INTERFACE void Plat_Free( void *ptr )
{
EnterCriticalSection( &g_AllocCS );
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
g_pMemAlloc->Free( ptr );
#else
free( ptr );
#endif
LeaveCriticalSection( &g_AllocCS );
}
#endif
#ifndef _X360
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
PLATFORM_INTERFACE void Plat_SetAllocErrorFn( Plat_AllocErrorFn fn )
{
g_AllocError = fn;
}
#endif
#endif
#endif