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.

1940 lines
50 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Insert this file into all projects using the memory system
  4. // It will cause that project to use the shader memory allocator
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifdef SN_TARGET_PS3
  9. #ifdef PS3MEMOVERRIDEWRAP
  10. /*
  11. ========= PS3 LAUNCHER MAIN ELF SECTION ==================
  12. ========= Vitaliy ======= May 2010 ===================
  13. The implementation of memory allocation wraps in the scenario
  14. of bootstrapping ELF and PRXs is as follows:
  15. ELF wraps all memory allocation CRT function here (see --wrap below)
  16. ELF lets all statically linked libraries to initialize before
  17. main function runs, it is required in order to be able to load
  18. any PRXs to initialize some CRT globals and sysmodules data.
  19. ELF tries to load tier0.prx containing the custom memory
  20. management allocator as soon as possible in main function.
  21. So this is when allocations can be happening:
  22. 1) global CRT contructors calling malloc (IOBUFs,mutexes,etc.)
  23. 2) main calling malloc indirectly trying to determine boot
  24. parameters and where to load tier0.prx module
  25. >>--->>-- here we already inject tier0 custom allocator!! <<----
  26. 3) global consturctors of tier0.prx before tier0 prx entry
  27. point function runs
  28. 4) tier0 prx entry point function returns to bootstrapper's main
  29. 5) everything else
  30. ****** WHAT HAPPENS BEFORE TIER0 CUSTOM ALLOCATOR IS AVAILABLE?
  31. Since all memory allocation calls are going into our custom
  32. wrapped malloc/free implementation that doesn't yet have real
  33. custom allocator, we create a static .bss section array (256Kb)
  34. and we just hand out 16-byte aligned addresses from this array.
  35. The memory allocated from this .bss section is essentially never
  36. freed. Test runs show that roughly 68Kb is actually allocated
  37. before tier0 custom allocator becomes available.
  38. ****** HOW DO WE INJECT TIER0 CUSTOM ALLOCATOR SO EARLY?
  39. For this we utilize two tricks.
  40. RUN SOME CODE IN TIER0 BEFORE EVERYTHING ELSE:
  41. We use attribute "init_priority(101)" on a global struct
  42. in tier0 to run its constructor function before any other
  43. global objects in tier0 construct.
  44. That function calls into ELF to obtain real addresses of
  45. real CRT functions, constructs the custom memory allocator
  46. and signals the ELF the pointer to allocator's interface,
  47. so all other tier0 global objects will already use the
  48. custom allocator's interface even though tier0's entry point
  49. didn't run yet.
  50. EARLY COMMUNICATION ELF-PRX BEFORE PRX ENTRY POINT
  51. We use CRT wrap function "malloc_stats", we call it with
  52. special parameters passing data from PRX into ELF wrapper.
  53. Return value of the function essentially passes data back
  54. from ELF into PRX thus allowing to handshake in the guts
  55. of PRX loading code between ELF calling prx_start_module
  56. and prx entry point.
  57. =============================================================
  58. We must be using --wrap=<symbol> on PS3 SNC compiler to wrap
  59. all calls to malloc-related functions.
  60. --wrap=<symbol>
  61. Use wrapper functions for <symbol>. If the linker is passed
  62. the argument '-wrap=foo', it will resolve references to the
  63. symbol foo to __wrap_foo; in addition, a new symbol __real_foo
  64. will be created which will resolve to the original foo.
  65. For example, by defining a function __wrap_fopen() that
  66. implements any desired wrapping logic and that invokes the
  67. real fopen() call through the __real_foo symbol, we can give
  68. the linker the argument '--wrap=fopen' and easily intercept
  69. calls to fopen().
  70. extern FILE *__real_fopen (const char * restrict filename, const char * restrict mode);
  71. FILE *__wrap_fopen (const char * restrict filename, const char * restrict mode)
  72. {
  73. printf ('open %s\n', filename);
  74. return __real_fopen (filename, mode);
  75. }
  76. */
  77. #define _PS3 1
  78. #include <stdlib.h>
  79. #include "memalloc.h"
  80. #include "dbg.h"
  81. #include "memoverride_ps3.h"
  82. #include "../steam/steam_platform_ps3/steamps3params.h"
  83. #define WRAP_FN_HOOK( name ) __wrap_##name
  84. #define WRAP_FN_REAL( name ) __real_##name
  85. static IMemAlloc *g_p_tier0_memalloc_interface = NULL;
  86. #ifdef g_pMemAlloc
  87. #undef g_pMemAlloc
  88. #endif
  89. #define g_pMemAlloc g_p_tier0_memalloc_interface
  90. char g_ps3_malloc_startup_buffer[ 256 * 1024 ];
  91. int g_ps3_malloc_startup_index = 0;
  92. inline void * MallocStartupBuffer()
  93. {
  94. return &g_ps3_malloc_startup_buffer[ g_ps3_malloc_startup_index ];
  95. }
  96. inline void MallocStartupUsed( size_t size )
  97. {
  98. size = ( size + 0xF )&~0xF; // round up to a multiple of 16 bytes
  99. g_ps3_malloc_startup_index += size;
  100. if ( g_ps3_malloc_startup_index > sizeof( g_ps3_malloc_startup_buffer ) )
  101. abort();
  102. }
  103. inline bool IsMallocStartupAllocation( void *p )
  104. {
  105. return ( p >= g_ps3_malloc_startup_buffer ) && ( p < &g_ps3_malloc_startup_buffer[ g_ps3_malloc_startup_index ] );
  106. }
  107. inline size_t GetMallocStartupAllocationSize( void *p )
  108. {
  109. char *pCh = ( char * ) p;
  110. pCh -= 16;
  111. size_t *pSize = reinterpret_cast< size_t * >( pCh );
  112. return *pSize;
  113. }
  114. MemOverrideRawCrtFunctions_t * GetRawCrtFunctions();
  115. SteamPS3Memory_t *GetSteamMemoryFunctions();
  116. _C_STD_BEGIN
  117. extern "C" {
  118. void WRAP_FN_REAL(abort)(void);
  119. void WRAP_FN_HOOK(abort)(void)
  120. {
  121. #ifndef _CERT
  122. DebuggerBreak();
  123. #endif
  124. WRAP_FN_REAL(abort)();
  125. }
  126. void * WRAP_FN_REAL(malloc)( size_t size );
  127. void * WRAP_FN_HOOK(malloc)( size_t size )
  128. {
  129. if ( g_pMemAlloc )
  130. {
  131. return g_pMemAlloc->Alloc( size );
  132. }
  133. else
  134. {
  135. size_t *pSize = (size_t*) MallocStartupBuffer();
  136. *pSize = size;
  137. MallocStartupUsed( sizeof( size_t ) );
  138. void *pvResult = MallocStartupBuffer();
  139. MallocStartupUsed( size );
  140. return pvResult;
  141. }
  142. }
  143. void * WRAP_FN_REAL(calloc)( size_t nelem, size_t size );
  144. void * WRAP_FN_HOOK(calloc)( size_t nelem, size_t size )
  145. {
  146. void *p = g_pMemAlloc ? g_pMemAlloc->Alloc( nelem * size ) : malloc( nelem * size );
  147. memset( p, 0, nelem * size );
  148. return p;
  149. }
  150. void * WRAP_FN_REAL(memalign)(size_t boundary, size_t size_arg);
  151. void * WRAP_FN_HOOK(memalign)(size_t boundary, size_t size_arg)
  152. {
  153. if ( g_pMemAlloc )
  154. {
  155. void * p = g_pMemAlloc->AllocAlign( size_arg, boundary );
  156. Assert( 0 == ( reinterpret_cast< size_t >( p ) % boundary ) );
  157. return p;
  158. }
  159. else
  160. {
  161. return malloc( size_arg );
  162. }
  163. }
  164. void * WRAP_FN_REAL(realloc)( void *p, size_t size_arg );
  165. void * WRAP_FN_HOOK(realloc)( void *p, size_t size_arg )
  166. {
  167. if ( !IsMallocStartupAllocation( p ) && g_pMemAlloc )
  168. {
  169. return g_pMemAlloc->Realloc( p, size_arg );
  170. }
  171. else
  172. {
  173. void *pNewData = malloc( size_arg );
  174. size_t nOldSize = GetMallocStartupAllocationSize( p );
  175. memcpy( pNewData, p, nOldSize < size_arg ? nOldSize : size_arg );
  176. free( p );
  177. return pNewData;
  178. }
  179. }
  180. void * WRAP_FN_REAL(reallocalign)(void *p, size_t size_arg, size_t boundary);
  181. void * WRAP_FN_HOOK(reallocalign)(void *p, size_t size_arg, size_t boundary)
  182. {
  183. if ( !IsMallocStartupAllocation( p ) && g_pMemAlloc )
  184. {
  185. p = g_pMemAlloc->ReallocAlign( p, size_arg, boundary );
  186. Assert( 0 == ( reinterpret_cast< size_t >( p ) % boundary ) );
  187. return p;
  188. }
  189. else
  190. {
  191. void *pNewData = memalign( boundary, size_arg );
  192. size_t nOldSize = GetMallocStartupAllocationSize( p );
  193. memcpy( pNewData, p, nOldSize < size_arg ? nOldSize : size_arg );
  194. free( p );
  195. return pNewData;
  196. }
  197. }
  198. size_t WRAP_FN_REAL(malloc_usable_size)( void *p );
  199. size_t WRAP_FN_HOOK(malloc_usable_size)( void *p )
  200. {
  201. if ( !IsMallocStartupAllocation( p ) && g_pMemAlloc )
  202. return g_pMemAlloc->GetSize( p );
  203. return GetMallocStartupAllocationSize( p );
  204. }
  205. int WRAP_FN_REAL(malloc_stats)(struct malloc_managed_size *mms);
  206. int WRAP_FN_HOOK(malloc_stats)(struct malloc_managed_size *mms)
  207. {
  208. if ( mms &&
  209. mms->current_inuse_size == 0x12345678 &&
  210. mms->current_system_size == 0x09ABCDEF )
  211. {
  212. // This is our passthrough function to establish
  213. // real memalloc interface from tier0
  214. g_pMemAlloc = reinterpret_cast< IMemAlloc * >( mms->max_system_size );
  215. return reinterpret_cast<int>( GetRawCrtFunctions() );
  216. }
  217. if ( mms &&
  218. mms->current_inuse_size == STEAMPS3_MALLOC_INUSE &&
  219. mms->current_system_size == STEAMPS3_MALLOC_SYSTEM )
  220. {
  221. // Steam is requesting memory struct
  222. mms->current_inuse_size = STEAMPS3_MALLOC_OK;
  223. return reinterpret_cast<int>( GetSteamMemoryFunctions() );
  224. }
  225. memset( mms, 0, sizeof( *mms ) );
  226. return 0;
  227. }
  228. void WRAP_FN_REAL(free)( void *p );
  229. void WRAP_FN_HOOK(free)( void *p )
  230. {
  231. if ( !IsMallocStartupAllocation( p ) && g_pMemAlloc )
  232. g_pMemAlloc->Free( p );
  233. }
  234. }
  235. _C_STD_END
  236. MemOverrideRawCrtFunctions_t * GetRawCrtFunctions()
  237. {
  238. using namespace std;
  239. static MemOverrideRawCrtFunctions_t g_rawfns =
  240. {
  241. WRAP_FN_REAL( malloc ),
  242. WRAP_FN_REAL( calloc ),
  243. WRAP_FN_REAL( memalign ),
  244. WRAP_FN_REAL( realloc ),
  245. WRAP_FN_REAL( reallocalign ),
  246. WRAP_FN_REAL( malloc_usable_size ),
  247. WRAP_FN_REAL( malloc_stats ),
  248. WRAP_FN_REAL( free )
  249. };
  250. return &g_rawfns;
  251. }
  252. SteamPS3Memory_t *GetSteamMemoryFunctions()
  253. {
  254. using namespace std;
  255. static SteamPS3Memory_t g_steamPS3Memory =
  256. {
  257. #if defined( STEAM_SHARES_GAME_ALLOCATOR )
  258. false, // false=[Steam and game share the tier0 memory allocator]
  259. #else
  260. true, // true=[Steam manages 6Mb chunk, allocated from the game on startup]
  261. #endif
  262. WRAP_FN_HOOK( malloc ),
  263. WRAP_FN_HOOK( realloc ),
  264. WRAP_FN_HOOK( free ),
  265. WRAP_FN_HOOK( malloc_usable_size )
  266. };
  267. return &g_steamPS3Memory;
  268. }
  269. #elif !defined( TIER0_DLL_EXPORT )
  270. /*
  271. ========= PS3 SECTION FOR ALL MODULES EXCEPT TIER0 ==================
  272. */
  273. #include "tier0/dbg.h"
  274. #include "tier0/memalloc.h"
  275. #if (defined(_DEBUG) || defined(USE_MEM_DEBUG))
  276. void *operator new( unsigned int nSize, int nBlockUse_UNUSED, const char *pFileName, int nLine )
  277. {
  278. return MemAlloc_Alloc( nSize, pFileName, nLine );
  279. }
  280. void *operator new[] ( unsigned int nSize, int nBlockUse_UNUSED, const char *pFileName, int nLine )
  281. {
  282. return MemAlloc_Alloc( nSize, pFileName, nLine );
  283. }
  284. #endif
  285. #endif // PS3MEMOVERRIDEWRAP
  286. #else
  287. #if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
  288. #define AVOID_INCLUDING_ALGORITHM
  289. #undef PROTECTED_THINGS_ENABLE // allow use of _vsnprintf
  290. #include <stdlib.h>
  291. #include "platform.h"
  292. extern "C" void __cdecl WriteMiniDump( void );
  293. inline void __cdecl VPurecallHandler()
  294. {
  295. DebuggerBreakIfDebugging(); // give the debugger a chance to catch first
  296. WriteMiniDump();
  297. Plat_ExitProcess( EXIT_FAILURE );
  298. }
  299. #if defined( _WIN32 ) && !defined( _X360 )
  300. #define WIN_32_LEAN_AND_MEAN
  301. #include <windows.h>
  302. // set Windows pure virtual handler
  303. _purecall_handler OldPurecallHandler = _set_purecall_handler( VPurecallHandler );
  304. #elif defined( POSIX ) && !defined( _PS3 )
  305. // set OSX/Linux pure virtual handler
  306. extern "C" void __cxa_pure_virtual() { VPurecallHandler(); }
  307. #endif
  308. #include "tier0/dbg.h"
  309. #include "tier0/memalloc.h"
  310. #include <string.h>
  311. #include <stdio.h>
  312. #include "memdbgoff.h"
  313. #ifdef _WIN32
  314. // ARG: crtdbg is necessary for certain definitions below,
  315. // but it also redefines malloc as a macro in release.
  316. // To disable this, we gotta define _DEBUG before including it.. BLEAH!
  317. #define _DEBUG 1
  318. #include "crtdbg.h"
  319. #ifdef NDEBUG
  320. #undef _DEBUG
  321. #endif
  322. // Turn this back off in release mode.
  323. #ifdef NDEBUG
  324. #undef _DEBUG
  325. #endif
  326. #elif POSIX
  327. #define __cdecl
  328. #endif
  329. #if defined(USE_MEM_DEBUG)
  330. #pragma optimize( "", off )
  331. #define inline
  332. #endif
  333. const char *g_pszModule = MKSTRING( MEMOVERRIDE_MODULE );
  334. inline void *AllocUnattributed( size_t nSize )
  335. {
  336. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  337. return MemAlloc_Alloc(nSize);
  338. #else
  339. return MemAlloc_Alloc(nSize, ::g_pszModule, 0);
  340. #endif
  341. }
  342. inline void *ReallocUnattributed( void *pMem, size_t nSize )
  343. {
  344. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  345. return g_pMemAlloc->Realloc(pMem, nSize);
  346. #else
  347. return g_pMemAlloc->Realloc(pMem, nSize, ::g_pszModule, 0);
  348. #endif
  349. }
  350. #undef inline
  351. //-----------------------------------------------------------------------------
  352. // Standard functions in the CRT that we're going to override to call our allocator
  353. //-----------------------------------------------------------------------------
  354. #if defined(_WIN32) && !defined(_STATIC_LINKED)
  355. // this magic only works under win32
  356. // under linux this malloc() overrides the libc malloc() and so we
  357. // end up in a recursion (as MemAlloc_Alloc() calls malloc)
  358. #if _MSC_VER >= 1400
  359. #if _MSC_VER >= 1900
  360. #define _CRTNOALIAS
  361. #endif
  362. #define ALLOC_CALL _CRTNOALIAS _CRTRESTRICT
  363. #define FREE_CALL _CRTNOALIAS
  364. #else
  365. #define ALLOC_CALL
  366. #define FREE_CALL
  367. #endif
  368. extern "C"
  369. {
  370. ALLOC_CALL void *malloc( size_t nSize )
  371. {
  372. return AllocUnattributed( nSize );
  373. }
  374. FREE_CALL void free( void *pMem )
  375. {
  376. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  377. g_pMemAlloc->Free(pMem);
  378. #else
  379. g_pMemAlloc->Free(pMem, ::g_pszModule, 0 );
  380. #endif
  381. }
  382. ALLOC_CALL void *realloc( void *pMem, size_t nSize )
  383. {
  384. return ReallocUnattributed( pMem, nSize );
  385. }
  386. ALLOC_CALL void *calloc( size_t nCount, size_t nElementSize )
  387. {
  388. void *pMem = AllocUnattributed( nElementSize * nCount );
  389. memset(pMem, 0, nElementSize * nCount);
  390. return pMem;
  391. }
  392. } // end extern "C"
  393. //-----------------------------------------------------------------------------
  394. // Non-standard MSVC functions that we're going to override to call our allocator
  395. //-----------------------------------------------------------------------------
  396. extern "C"
  397. {
  398. // 64-bit
  399. #ifdef _WIN64
  400. void* __cdecl _malloc_base( size_t nSize )
  401. {
  402. return AllocUnattributed( nSize );
  403. }
  404. #else
  405. void *_malloc_base( size_t nSize )
  406. {
  407. return AllocUnattributed( nSize );
  408. }
  409. #endif
  410. #if ( defined ( _MSC_VER ) && _MSC_VER >= 1900 )
  411. _CRTRESTRICT void *_calloc_base(size_t nCount, size_t nSize)
  412. {
  413. void *pMem = AllocUnattributed(nCount*nSize);
  414. memset(pMem, 0, nCount*nSize);
  415. return pMem;
  416. }
  417. #else
  418. void *_calloc_base( size_t nSize )
  419. {
  420. void *pMem = AllocUnattributed( nSize );
  421. memset(pMem, 0, nSize);
  422. return pMem;
  423. }
  424. #endif
  425. void *_realloc_base( void *pMem, size_t nSize )
  426. {
  427. return ReallocUnattributed( pMem, nSize );
  428. }
  429. void *_recalloc_base( void *pMem, size_t nSize )
  430. {
  431. void *pMemOut = ReallocUnattributed( pMem, nSize );
  432. if (!pMem)
  433. {
  434. memset(pMemOut, 0, nSize);
  435. }
  436. return pMemOut;
  437. }
  438. void _free_base( void *pMem )
  439. {
  440. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  441. g_pMemAlloc->Free(pMem);
  442. #else
  443. g_pMemAlloc->Free(pMem, ::g_pszModule, 0 );
  444. #endif
  445. }
  446. void *__cdecl _expand_base( void *pMem, size_t nNewSize, int nBlockUse )
  447. {
  448. Assert( 0 );
  449. return NULL;
  450. }
  451. // crt
  452. void * __cdecl _malloc_crt(size_t size)
  453. {
  454. return AllocUnattributed( size );
  455. }
  456. void * __cdecl _calloc_crt(size_t count, size_t size)
  457. {
  458. #if (defined( _MSC_VER ) && _MSC_VER >= 1900)
  459. return _calloc_base(count, size);
  460. #else
  461. return _calloc_base( count * size );
  462. #endif
  463. }
  464. void * __cdecl _realloc_crt(void *ptr, size_t size)
  465. {
  466. return _realloc_base( ptr, size );
  467. }
  468. void * __cdecl _recalloc_crt(void *ptr, size_t count, size_t size)
  469. {
  470. return _recalloc_base( ptr, size * count );
  471. }
  472. ALLOC_CALL void * __cdecl _recalloc ( void * memblock, size_t count, size_t size )
  473. {
  474. void *pMem = ReallocUnattributed( memblock, size * count );
  475. if (!memblock)
  476. {
  477. memset(pMem, 0, size * count);
  478. }
  479. return pMem;
  480. }
  481. size_t _msize_base( void *pMem )
  482. {
  483. return g_pMemAlloc->GetSize(pMem);
  484. }
  485. size_t _msize( void *pMem )
  486. {
  487. return _msize_base(pMem);
  488. }
  489. size_t msize( void *pMem )
  490. {
  491. return g_pMemAlloc->GetSize(pMem);
  492. }
  493. void *__cdecl _heap_alloc( size_t nSize )
  494. {
  495. return AllocUnattributed( nSize );
  496. }
  497. void *__cdecl _nh_malloc( size_t nSize, int )
  498. {
  499. return AllocUnattributed( nSize );
  500. }
  501. void *__cdecl _expand( void *pMem, size_t nSize )
  502. {
  503. Assert( 0 );
  504. return NULL;
  505. }
  506. unsigned int _amblksiz = 16; //BYTES_PER_PARA;
  507. #if _MSC_VER >= 1400
  508. HANDLE _crtheap = (HANDLE)1; // PatM Can't be 0 or CRT pukes
  509. int __active_heap = 1;
  510. #endif // _MSC_VER >= 1400
  511. size_t __cdecl _get_sbh_threshold( void )
  512. {
  513. return 0;
  514. }
  515. int __cdecl _set_sbh_threshold( size_t )
  516. {
  517. return 0;
  518. }
  519. int _heapchk()
  520. {
  521. return g_pMemAlloc->heapchk();
  522. }
  523. int _heapmin()
  524. {
  525. return 1;
  526. }
  527. int __cdecl _heapadd( void *, size_t )
  528. {
  529. return 0;
  530. }
  531. int __cdecl _heapset( unsigned int )
  532. {
  533. return 0;
  534. }
  535. size_t __cdecl _heapused( size_t *, size_t * )
  536. {
  537. return 0;
  538. }
  539. #ifdef _WIN32
  540. #include <malloc.h>
  541. int __cdecl _heapwalk( _HEAPINFO * )
  542. {
  543. return 0;
  544. }
  545. #endif
  546. } // end extern "C"
  547. //-----------------------------------------------------------------------------
  548. // Debugging functions that we're going to override to call our allocator
  549. // NOTE: These have to be here for release + debug builds in case we
  550. // link to a debug static lib!!!
  551. //-----------------------------------------------------------------------------
  552. extern "C"
  553. {
  554. void *malloc_db( size_t nSize, const char *pFileName, int nLine )
  555. {
  556. return MemAlloc_Alloc(nSize, pFileName, nLine);
  557. }
  558. void free_db( void *pMem, const char *pFileName, int nLine )
  559. {
  560. g_pMemAlloc->Free(pMem, pFileName, nLine);
  561. }
  562. void *realloc_db( void *pMem, size_t nSize, const char *pFileName, int nLine )
  563. {
  564. return g_pMemAlloc->Realloc(pMem, nSize, pFileName, nLine);
  565. }
  566. } // end extern "C"
  567. //-----------------------------------------------------------------------------
  568. // These methods are standard MSVC heap initialization + shutdown methods
  569. //-----------------------------------------------------------------------------
  570. extern "C"
  571. {
  572. #if !defined( _X360 )
  573. int __cdecl _heap_init()
  574. {
  575. return g_pMemAlloc != NULL;
  576. }
  577. void __cdecl _heap_term()
  578. {
  579. }
  580. #endif
  581. }
  582. #endif
  583. //-----------------------------------------------------------------------------
  584. // Prevents us from using an inappropriate new or delete method,
  585. // ensures they are here even when linking against debug or release static libs
  586. //-----------------------------------------------------------------------------
  587. #ifndef NO_MEMOVERRIDE_NEW_DELETE
  588. #if !defined( _OSX )
  589. void *__cdecl operator new( size_t nSize )
  590. {
  591. return AllocUnattributed( nSize );
  592. }
  593. void *__cdecl operator new( size_t nSize, int nBlockUse, const char *pFileName, int nLine )
  594. {
  595. return MemAlloc_Alloc(nSize, pFileName, nLine );
  596. }
  597. void *__cdecl operator new[] ( size_t nSize )
  598. {
  599. return AllocUnattributed( nSize );
  600. }
  601. void *__cdecl operator new[] ( size_t nSize, int nBlockUse, const char *pFileName, int nLine )
  602. {
  603. return MemAlloc_Alloc(nSize, pFileName, nLine);
  604. }
  605. #else
  606. void *__cdecl operator new( size_t nSize ) throw (std::bad_alloc)
  607. {
  608. return AllocUnattributed( nSize );
  609. }
  610. void *__cdecl operator new( size_t nSize, int nBlockUse, const char *pFileName, int nLine )
  611. {
  612. return MemAlloc_Alloc(nSize, pFileName, nLine );
  613. }
  614. void *__cdecl operator new[] ( size_t nSize ) throw (std::bad_alloc)
  615. {
  616. return AllocUnattributed( nSize );
  617. }
  618. void *__cdecl operator new[] ( size_t nSize, int nBlockUse, const char *pFileName, int nLine )
  619. {
  620. return MemAlloc_Alloc(nSize, pFileName, nLine);
  621. }
  622. #endif // !_OSX
  623. void __cdecl operator delete( void *pMem ) throw()
  624. {
  625. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  626. g_pMemAlloc->Free(pMem);
  627. #else
  628. g_pMemAlloc->Free(pMem, ::g_pszModule, 0 );
  629. #endif
  630. }
  631. void __cdecl operator delete[] ( void *pMem ) throw()
  632. {
  633. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  634. g_pMemAlloc->Free(pMem);
  635. #else
  636. g_pMemAlloc->Free(pMem, ::g_pszModule, 0 );
  637. #endif
  638. }
  639. #endif
  640. //-----------------------------------------------------------------------------
  641. // Override some debugging allocation methods in MSVC
  642. // NOTE: These have to be here for release + debug builds in case we
  643. // link to a debug static lib!!!
  644. //-----------------------------------------------------------------------------
  645. #ifndef _STATIC_LINKED
  646. #ifdef _WIN32
  647. // This here just hides the internal file names, etc of allocations
  648. // made in the c runtime library
  649. #define CRT_INTERNAL_FILE_NAME "C-runtime internal"
  650. class CAttibCRT
  651. {
  652. public:
  653. CAttibCRT(int nBlockUse) : m_nBlockUse(nBlockUse)
  654. {
  655. if (m_nBlockUse == _CRT_BLOCK)
  656. {
  657. g_pMemAlloc->PushAllocDbgInfo(CRT_INTERNAL_FILE_NAME, 0);
  658. }
  659. }
  660. ~CAttibCRT()
  661. {
  662. if (m_nBlockUse == _CRT_BLOCK)
  663. {
  664. g_pMemAlloc->PopAllocDbgInfo();
  665. }
  666. }
  667. private:
  668. int m_nBlockUse;
  669. };
  670. #define AttribIfCrt() CAttibCRT _attrib(nBlockUse)
  671. #elif defined(POSIX) || defined( _PS3 )
  672. #define AttribIfCrt()
  673. #endif // _WIN32
  674. extern "C"
  675. {
  676. void *__cdecl _nh_malloc_dbg( size_t nSize, int nFlag, int nBlockUse,
  677. const char *pFileName, int nLine )
  678. {
  679. AttribIfCrt();
  680. return MemAlloc_Alloc(nSize, pFileName, nLine);
  681. }
  682. void *__cdecl _malloc_dbg( size_t nSize, int nBlockUse,
  683. const char *pFileName, int nLine )
  684. {
  685. AttribIfCrt();
  686. return MemAlloc_Alloc(nSize, pFileName, nLine);
  687. }
  688. #if ( defined(_MSC_VER) && ( _MSC_VER >= 1600 ) ) || defined( _X360 )
  689. void *__cdecl _calloc_dbg_impl( size_t nNum, size_t nSize, int nBlockUse,
  690. const char * szFileName, int nLine, int * errno_tmp )
  691. {
  692. return _calloc_dbg( nNum, nSize, nBlockUse, szFileName, nLine );
  693. }
  694. #endif
  695. void *__cdecl _calloc_dbg( size_t nNum, size_t nSize, int nBlockUse,
  696. const char *pFileName, int nLine )
  697. {
  698. AttribIfCrt();
  699. void *pMem = MemAlloc_Alloc(nSize * nNum, pFileName, nLine);
  700. memset(pMem, 0, nSize * nNum);
  701. return pMem;
  702. }
  703. void *__cdecl _realloc_dbg( void *pMem, size_t nNewSize, int nBlockUse,
  704. const char *pFileName, int nLine )
  705. {
  706. AttribIfCrt();
  707. return g_pMemAlloc->Realloc(pMem, nNewSize, pFileName, nLine);
  708. }
  709. void *__cdecl _expand_dbg( void *pMem, size_t nNewSize, int nBlockUse,
  710. const char *pFileName, int nLine )
  711. {
  712. Assert( 0 );
  713. return NULL;
  714. }
  715. void __cdecl _free_dbg( void *pMem, int nBlockUse )
  716. {
  717. AttribIfCrt();
  718. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  719. g_pMemAlloc->Free(pMem);
  720. #else
  721. g_pMemAlloc->Free(pMem, ::g_pszModule, 0 );
  722. #endif
  723. }
  724. size_t __cdecl _msize_dbg( void *pMem, int nBlockUse )
  725. {
  726. #ifdef _WIN32
  727. return _msize(pMem);
  728. #elif POSIX || _PS3
  729. Assert( "_msize_dbg unsupported" );
  730. return 0;
  731. #endif
  732. }
  733. #ifdef _WIN32
  734. #if defined(_DEBUG) && _MSC_VER >= 1300
  735. // X360TBD: aligned and offset allocations may be important on the 360
  736. // aligned base
  737. ALLOC_CALL void *__cdecl _aligned_malloc_base( size_t size, size_t align )
  738. {
  739. return MemAlloc_AllocAligned( size, align );
  740. }
  741. ALLOC_CALL void *__cdecl _aligned_realloc_base( void *ptr, size_t size, size_t align )
  742. {
  743. return MemAlloc_ReallocAligned( ptr, size, align );
  744. }
  745. ALLOC_CALL void *__cdecl _aligned_recalloc_base( void *ptr, size_t size, size_t align )
  746. {
  747. Error( "Unsupported function\n" );
  748. return NULL;
  749. }
  750. FREE_CALL void __cdecl _aligned_free_base( void *ptr )
  751. {
  752. MemAlloc_FreeAligned( ptr );
  753. }
  754. // aligned
  755. ALLOC_CALL void * __cdecl _aligned_malloc( size_t size, size_t align )
  756. {
  757. return _aligned_malloc_base(size, align);
  758. }
  759. ALLOC_CALL void *__cdecl _aligned_realloc(void *memblock, size_t size, size_t align)
  760. {
  761. return _aligned_realloc_base(memblock, size, align);
  762. }
  763. ALLOC_CALL void * __cdecl _aligned_recalloc( void * memblock, size_t count, size_t size, size_t align )
  764. {
  765. return _aligned_recalloc_base(memblock, count * size, align);
  766. }
  767. FREE_CALL void __cdecl _aligned_free( void *memblock )
  768. {
  769. _aligned_free_base(memblock);
  770. }
  771. // aligned offset base
  772. ALLOC_CALL void * __cdecl _aligned_offset_malloc_base( size_t size, size_t align, size_t offset )
  773. {
  774. Assert( IsPC() || 0 );
  775. return NULL;
  776. }
  777. ALLOC_CALL void * __cdecl _aligned_offset_realloc_base( void * memblock, size_t size, size_t align, size_t offset)
  778. {
  779. Assert( IsPC() || 0 );
  780. return NULL;
  781. }
  782. ALLOC_CALL void * __cdecl _aligned_offset_recalloc_base( void * memblock, size_t size, size_t align, size_t offset)
  783. {
  784. Assert( IsPC() || 0 );
  785. return NULL;
  786. }
  787. // aligned offset
  788. ALLOC_CALL void *__cdecl _aligned_offset_malloc(size_t size, size_t align, size_t offset)
  789. {
  790. return _aligned_offset_malloc_base( size, align, offset );
  791. }
  792. ALLOC_CALL void *__cdecl _aligned_offset_realloc(void *memblock, size_t size, size_t align, size_t offset)
  793. {
  794. return _aligned_offset_realloc_base( memblock, size, align, offset );
  795. }
  796. ALLOC_CALL void * __cdecl _aligned_offset_recalloc( void * memblock, size_t count, size_t size, size_t align, size_t offset )
  797. {
  798. return _aligned_offset_recalloc_base( memblock, count * size, align, offset );
  799. }
  800. #endif // _MSC_VER >= 1400
  801. #endif
  802. } // end extern "C"
  803. //-----------------------------------------------------------------------------
  804. // Override some the _CRT debugging allocation methods in MSVC
  805. //-----------------------------------------------------------------------------
  806. #ifdef _WIN32
  807. extern "C"
  808. {
  809. int _CrtDumpMemoryLeaks(void)
  810. {
  811. return 0;
  812. }
  813. _CRT_DUMP_CLIENT _CrtSetDumpClient( _CRT_DUMP_CLIENT dumpClient )
  814. {
  815. return NULL;
  816. }
  817. int _CrtSetDbgFlag( int nNewFlag )
  818. {
  819. return g_pMemAlloc->CrtSetDbgFlag( nNewFlag );
  820. }
  821. // 64-bit port.
  822. #define AFNAME(var) __p_ ## var
  823. #define AFRET(var) &var
  824. #if ( defined( _MSC_VER ) && _MSC_VER >= 1900)
  825. //Do we need to do anything here for VS2015?
  826. #else
  827. int _crtDbgFlag = _CRTDBG_ALLOC_MEM_DF;
  828. int* AFNAME(_crtDbgFlag)(void)
  829. {
  830. return AFRET(_crtDbgFlag);
  831. }
  832. long _crtBreakAlloc; /* Break on this allocation */
  833. long* AFNAME(_crtBreakAlloc) (void)
  834. {
  835. return AFRET(_crtBreakAlloc);
  836. }
  837. #endif
  838. void __cdecl _CrtSetDbgBlockType( void *pMem, int nBlockUse )
  839. {
  840. DebuggerBreak();
  841. }
  842. _CRT_ALLOC_HOOK __cdecl _CrtSetAllocHook( _CRT_ALLOC_HOOK pfnNewHook )
  843. {
  844. DebuggerBreak();
  845. return NULL;
  846. }
  847. long __cdecl _CrtSetBreakAlloc( long lNewBreakAlloc )
  848. {
  849. return g_pMemAlloc->CrtSetBreakAlloc( lNewBreakAlloc );
  850. }
  851. int __cdecl _CrtIsValidHeapPointer( const void *pMem )
  852. {
  853. return g_pMemAlloc->CrtIsValidHeapPointer( pMem );
  854. }
  855. int __cdecl _CrtIsValidPointer( const void *pMem, unsigned int size, int access )
  856. {
  857. return g_pMemAlloc->CrtIsValidPointer( pMem, size, access );
  858. }
  859. int __cdecl _CrtCheckMemory( void )
  860. {
  861. // FIXME: Remove this when we re-implement the heap
  862. return g_pMemAlloc->CrtCheckMemory( );
  863. }
  864. int __cdecl _CrtIsMemoryBlock( const void *pMem, unsigned int nSize,
  865. long *plRequestNumber, char **ppFileName, int *pnLine )
  866. {
  867. DebuggerBreak();
  868. return 1;
  869. }
  870. int __cdecl _CrtMemDifference( _CrtMemState *pState, const _CrtMemState * oldState, const _CrtMemState * newState )
  871. {
  872. DebuggerBreak();
  873. return FALSE;
  874. }
  875. void __cdecl _CrtMemDumpStatistics( const _CrtMemState *pState )
  876. {
  877. DebuggerBreak();
  878. }
  879. void __cdecl _CrtMemCheckpoint( _CrtMemState *pState )
  880. {
  881. // FIXME: Remove this when we re-implement the heap
  882. g_pMemAlloc->CrtMemCheckpoint( pState );
  883. }
  884. void __cdecl _CrtMemDumpAllObjectsSince( const _CrtMemState *pState )
  885. {
  886. DebuggerBreak();
  887. }
  888. void __cdecl _CrtDoForAllClientObjects( void (*pfn)(void *, void *), void * pContext )
  889. {
  890. DebuggerBreak();
  891. }
  892. //-----------------------------------------------------------------------------
  893. // Methods in dbgrpt.cpp
  894. //-----------------------------------------------------------------------------
  895. long _crtAssertBusy = -1;
  896. int __cdecl _CrtSetReportMode( int nReportType, int nReportMode )
  897. {
  898. return g_pMemAlloc->CrtSetReportMode( nReportType, nReportMode );
  899. }
  900. _HFILE __cdecl _CrtSetReportFile( int nRptType, _HFILE hFile )
  901. {
  902. return (_HFILE)g_pMemAlloc->CrtSetReportFile( nRptType, hFile );
  903. }
  904. _CRT_REPORT_HOOK __cdecl _CrtSetReportHook( _CRT_REPORT_HOOK pfnNewHook )
  905. {
  906. return (_CRT_REPORT_HOOK)g_pMemAlloc->CrtSetReportHook( pfnNewHook );
  907. }
  908. int __cdecl _CrtDbgReport( int nRptType, const char * szFile,
  909. int nLine, const char * szModule, const char * szFormat, ... )
  910. {
  911. static char output[1024];
  912. va_list args;
  913. va_start( args, szFormat );
  914. _vsnprintf( output, sizeof( output )-1, szFormat, args );
  915. va_end( args );
  916. return g_pMemAlloc->CrtDbgReport( nRptType, szFile, nLine, szModule, output );
  917. }
  918. #if _MSC_VER >= 1400
  919. #if defined( _DEBUG ) && _MSC_VER < 1900
  920. // wrapper which passes no debug info; not available in debug
  921. void __cdecl _invalid_parameter_noinfo(void)
  922. {
  923. Assert(0);
  924. }
  925. #endif /* defined( _DEBUG ) */
  926. #if defined( _DEBUG ) || defined( USE_MEM_DEBUG )
  927. int __cdecl __crtMessageWindowW( int nRptType, const wchar_t * szFile, const wchar_t * szLine,
  928. const wchar_t * szModule, const wchar_t * szUserMessage )
  929. {
  930. Assert(0);
  931. return 0;
  932. }
  933. int __cdecl _CrtDbgReportV( int nRptType, const wchar_t *szFile, int nLine,
  934. const wchar_t *szModule, const wchar_t *szFormat, va_list arglist )
  935. {
  936. Assert(0);
  937. return 0;
  938. }
  939. int __cdecl _CrtDbgReportW( int nRptType, const wchar_t *szFile, int nLine,
  940. const wchar_t *szModule, const wchar_t *szFormat, ...)
  941. {
  942. Assert(0);
  943. return 0;
  944. }
  945. #if ( defined(_MSC_VER) && _MSC_VER >= 1900)
  946. int __cdecl _VCrtDbgReportA(int nRptType, void *pReturnAddr, const char* szFile, int nLine,
  947. const char *szModule, const char *szFormat, va_list arglist)
  948. {
  949. Assert(0);
  950. return 0;
  951. }
  952. #else
  953. int __cdecl _VCrtDbgReportA( int nRptType, const wchar_t * szFile, int nLine,
  954. const wchar_t * szModule, const wchar_t * szFormat, va_list arglist )
  955. {
  956. Assert(0);
  957. return 0;
  958. }
  959. #endif
  960. int __cdecl _CrtSetReportHook2( int mode, _CRT_REPORT_HOOK pfnNewHook )
  961. {
  962. _CrtSetReportHook( pfnNewHook );
  963. return 0;
  964. }
  965. #endif /* defined( _DEBUG ) || defined( USE_MEM_DEBUG ) */
  966. extern "C" int __crtDebugCheckCount = FALSE;
  967. extern "C" int __cdecl _CrtSetCheckCount( int fCheckCount )
  968. {
  969. int oldCheckCount = __crtDebugCheckCount;
  970. return oldCheckCount;
  971. }
  972. extern "C" int __cdecl _CrtGetCheckCount( void )
  973. {
  974. return __crtDebugCheckCount;
  975. }
  976. // aligned offset debug
  977. extern "C" void * __cdecl _aligned_offset_recalloc_dbg( void * memblock, size_t count, size_t size, size_t align, size_t offset, const char * f_name, int line_n )
  978. {
  979. Assert( IsPC() || 0 );
  980. void *pMem = ReallocUnattributed( memblock, size * count );
  981. if (!memblock)
  982. {
  983. memset(pMem, 0, size * count);
  984. }
  985. return pMem;
  986. }
  987. extern "C" void * __cdecl _aligned_recalloc_dbg( void *memblock, size_t count, size_t size, size_t align, const char * f_name, int line_n )
  988. {
  989. return _aligned_offset_recalloc_dbg(memblock, count, size, align, 0, f_name, line_n);
  990. }
  991. extern "C" void * __cdecl _recalloc_dbg ( void * memblock, size_t count, size_t size, int nBlockUse, const char * szFileName, int nLine )
  992. {
  993. return _aligned_offset_recalloc_dbg(memblock, count, size, 0, 0, szFileName, nLine);
  994. }
  995. _CRT_REPORT_HOOK __cdecl _CrtGetReportHook( void )
  996. {
  997. return NULL;
  998. }
  999. #endif
  1000. int __cdecl _CrtReportBlockType(const void * pUserData)
  1001. {
  1002. return 0;
  1003. }
  1004. } // end extern "C"
  1005. #endif // _WIN32
  1006. // Most files include this file, so when it's used it adds an extra .ValveDbg section,
  1007. // to help identify debug binaries.
  1008. #ifdef _WIN32
  1009. #ifndef NDEBUG // _DEBUG
  1010. #pragma data_seg("ValveDBG")
  1011. volatile const char* DBG = "*** DEBUG STUB ***";
  1012. #endif
  1013. #endif
  1014. #endif
  1015. // Extras added prevent dbgheap.obj from being included - DAL
  1016. #ifdef _WIN32
  1017. extern "C"
  1018. {
  1019. size_t __crtDebugFillThreshold = 0;
  1020. extern "C" void * __cdecl _heap_alloc_base (size_t size)
  1021. {
  1022. Assert(0);
  1023. return NULL;
  1024. }
  1025. void * __cdecl _heap_alloc_dbg( size_t nSize, int nBlockUse, const char * szFileName, int nLine)
  1026. {
  1027. return _heap_alloc(nSize);
  1028. }
  1029. // 64-bit
  1030. #ifdef _WIN64
  1031. static void * __cdecl realloc_help( void * pUserData, size_t * pnNewSize, int nBlockUse,const char * szFileName,
  1032. int nLine, int fRealloc )
  1033. {
  1034. Assert(0); // Shouldn't be needed
  1035. return NULL;
  1036. }
  1037. #else
  1038. static void * __cdecl realloc_help( void * pUserData, size_t nNewSize, int nBlockUse, const char * szFileName,
  1039. int nLine, int fRealloc)
  1040. {
  1041. Assert(0); // Shouldn't be needed
  1042. return NULL;
  1043. }
  1044. #endif
  1045. void __cdecl _free_nolock( void * pUserData)
  1046. {
  1047. // I don't think the second param is used in memoverride
  1048. _free_dbg(pUserData, 0);
  1049. }
  1050. void __cdecl _free_dbg_nolock( void * pUserData, int nBlockUse)
  1051. {
  1052. _free_dbg(pUserData, 0);
  1053. }
  1054. _CRT_ALLOC_HOOK __cdecl _CrtGetAllocHook ( void)
  1055. {
  1056. Assert(0);
  1057. return NULL;
  1058. }
  1059. static int __cdecl CheckBytes( unsigned char * pb, unsigned char bCheck, size_t nSize)
  1060. {
  1061. int bOkay = TRUE;
  1062. return bOkay;
  1063. }
  1064. _CRT_DUMP_CLIENT __cdecl _CrtGetDumpClient ( void)
  1065. {
  1066. Assert(0);
  1067. return NULL;
  1068. }
  1069. #if _MSC_VER >= 1400
  1070. static void __cdecl _printMemBlockData( _locale_t plocinfo, _CrtMemBlockHeader * pHead)
  1071. {
  1072. }
  1073. static void __cdecl _CrtMemDumpAllObjectsSince_stat( const _CrtMemState * state, _locale_t plocinfo)
  1074. {
  1075. }
  1076. #endif
  1077. void * __cdecl _aligned_malloc_dbg( size_t size, size_t align, const char * f_name, int line_n)
  1078. {
  1079. return _aligned_malloc(size, align);
  1080. }
  1081. void * __cdecl _aligned_realloc_dbg( void *memblock, size_t size, size_t align,
  1082. const char * f_name, int line_n)
  1083. {
  1084. return _aligned_realloc(memblock, size, align);
  1085. }
  1086. void * __cdecl _aligned_offset_malloc_dbg( size_t size, size_t align, size_t offset,
  1087. const char * f_name, int line_n)
  1088. {
  1089. return _aligned_offset_malloc(size, align, offset);
  1090. }
  1091. void * __cdecl _aligned_offset_realloc_dbg( void * memblock, size_t size, size_t align,
  1092. size_t offset, const char * f_name, int line_n)
  1093. {
  1094. return _aligned_offset_realloc(memblock, size, align, offset);
  1095. }
  1096. void __cdecl _aligned_free_dbg( void * memblock)
  1097. {
  1098. _aligned_free(memblock);
  1099. }
  1100. #if _MSC_VER < 1900
  1101. size_t __cdecl _CrtSetDebugFillThreshold( size_t _NewDebugFillThreshold)
  1102. {
  1103. Assert(0);
  1104. return 0;
  1105. }
  1106. #endif
  1107. //===========================================
  1108. // NEW!!! 64-bit
  1109. #if (_MSC_VER < 1900) || !defined( _DEBUG )
  1110. char * __cdecl _strdup ( const char * string )
  1111. {
  1112. int nSize = strlen(string) + 1;
  1113. char *pCopy = (char*)AllocUnattributed( nSize );
  1114. if ( pCopy )
  1115. memcpy( pCopy, string, nSize );
  1116. return pCopy;
  1117. }
  1118. #endif
  1119. #if 0
  1120. _TSCHAR * __cdecl _tfullpath_dbg ( _TSCHAR *UserBuf, const _TSCHAR *path, size_t maxlen, int nBlockUse, const char * szFileName, int nLine )
  1121. {
  1122. Assert(0);
  1123. return NULL;
  1124. }
  1125. _TSCHAR * __cdecl _tfullpath ( _TSCHAR *UserBuf, const _TSCHAR *path, size_t maxlen )
  1126. {
  1127. Assert(0);
  1128. return NULL;
  1129. }
  1130. _TSCHAR * __cdecl _tgetdcwd_lk_dbg ( int drive, _TSCHAR *pnbuf, int maxlen, int nBlockUse, const char * szFileName, int nLine )
  1131. {
  1132. Assert(0);
  1133. return NULL;
  1134. }
  1135. _TSCHAR * __cdecl _tgetdcwd_nolock ( int drive, _TSCHAR *pnbuf, int maxlen )
  1136. {
  1137. Assert(0);
  1138. return NULL;
  1139. }
  1140. errno_t __cdecl _tdupenv_s_helper ( _TSCHAR **pBuffer, size_t *pBufferSizeInTChars, const _TSCHAR *varname, int nBlockUse, const char * szFileName, int nLine )
  1141. {
  1142. Assert(0);
  1143. return 0;
  1144. }
  1145. errno_t __cdecl _tdupenv_s_helper ( _TSCHAR **pBuffer, size_t *pBufferSizeInTChars, const _TSCHAR *varname )
  1146. {
  1147. Assert(0);
  1148. return 0;
  1149. }
  1150. _TSCHAR * __cdecl _ttempnam_dbg ( const _TSCHAR *dir, const _TSCHAR *pfx, int nBlockUse, const char * szFileName, int nLine )
  1151. {
  1152. Assert(0);
  1153. return 0;
  1154. }
  1155. _TSCHAR * __cdecl _ttempnam ( const _TSCHAR *dir, const _TSCHAR *pfx )
  1156. {
  1157. Assert(0);
  1158. return 0;
  1159. }
  1160. wchar_t * __cdecl _wcsdup_dbg ( const wchar_t * string, int nBlockUse, const char * szFileName, int nLine )
  1161. {
  1162. Assert(0);
  1163. return 0;
  1164. }
  1165. wchar_t * __cdecl _wcsdup ( const wchar_t * string )
  1166. {
  1167. Assert(0);
  1168. return 0;
  1169. }
  1170. #endif
  1171. } // end extern "C"
  1172. #if _MSC_VER >= 1400
  1173. //-----------------------------------------------------------------------------
  1174. // XBox Memory Allocator Override
  1175. //-----------------------------------------------------------------------------
  1176. #if defined( _X360 )
  1177. #if defined( _DEBUG ) || defined( USE_MEM_DEBUG )
  1178. #include "utlmap.h"
  1179. MEMALLOC_DEFINE_EXTERNAL_TRACKING( XMem );
  1180. CThreadFastMutex g_XMemAllocMutex;
  1181. void XMemAlloc_RegisterAllocation( void *p, DWORD dwAllocAttributes )
  1182. {
  1183. if ( !g_pMemAlloc )
  1184. {
  1185. // core xallocs cannot be journaled until system is ready
  1186. return;
  1187. }
  1188. AUTO_LOCK_FM( g_XMemAllocMutex );
  1189. int size = XMemSize( p, dwAllocAttributes );
  1190. MemAlloc_RegisterExternalAllocation( XMem, p, size );
  1191. }
  1192. void XMemAlloc_RegisterDeallocation( void *p, DWORD dwAllocAttributes )
  1193. {
  1194. if ( !g_pMemAlloc )
  1195. {
  1196. // core xallocs cannot be journaled until system is ready
  1197. return;
  1198. }
  1199. AUTO_LOCK_FM( g_XMemAllocMutex );
  1200. int size = XMemSize( p, dwAllocAttributes );
  1201. MemAlloc_RegisterExternalDeallocation( XMem, p, size );
  1202. }
  1203. #else
  1204. #define XMemAlloc_RegisterAllocation( p, a ) ((void)0)
  1205. #define XMemAlloc_RegisterDeallocation( p, a ) ((void)0)
  1206. #endif
  1207. //-----------------------------------------------------------------------------
  1208. // XMemAlloc
  1209. //
  1210. // XBox Memory Allocator Override
  1211. //-----------------------------------------------------------------------------
  1212. LPVOID WINAPI XMemAlloc( SIZE_T dwSize, DWORD dwAllocAttributes )
  1213. {
  1214. LPVOID ptr;
  1215. XALLOC_ATTRIBUTES *pAttribs = (XALLOC_ATTRIBUTES *)&dwAllocAttributes;
  1216. bool bPhysical = ( pAttribs->dwMemoryType == XALLOC_MEMTYPE_PHYSICAL );
  1217. if ( !bPhysical && !pAttribs->dwHeapTracksAttributes && pAttribs->dwAllocatorId != eXALLOCAllocatorId_XUI )
  1218. {
  1219. MEM_ALLOC_CREDIT();
  1220. switch ( pAttribs->dwAlignment )
  1221. {
  1222. case XALLOC_ALIGNMENT_4:
  1223. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  1224. ptr = MemAlloc_Alloc( dwSize );
  1225. #else
  1226. ptr = MemAlloc_Alloc(dwSize, ::g_pszModule, 0);
  1227. #endif
  1228. break;
  1229. case XALLOC_ALIGNMENT_8:
  1230. ptr = MemAlloc_AllocAligned( dwSize, 8 );
  1231. break;
  1232. case XALLOC_ALIGNMENT_DEFAULT:
  1233. case XALLOC_ALIGNMENT_16:
  1234. default:
  1235. ptr = MemAlloc_AllocAligned( dwSize, 16 );
  1236. break;
  1237. }
  1238. if ( pAttribs->dwZeroInitialize != 0 )
  1239. {
  1240. memset( ptr, 0, XMemSize( ptr, dwAllocAttributes ) );
  1241. }
  1242. return ptr;
  1243. }
  1244. ptr = XMemAllocDefault( dwSize, dwAllocAttributes );
  1245. if ( ptr )
  1246. {
  1247. XMemAlloc_RegisterAllocation( ptr, dwAllocAttributes );
  1248. }
  1249. return ptr;
  1250. }
  1251. //-----------------------------------------------------------------------------
  1252. // XMemFree
  1253. //
  1254. // XBox Memory Allocator Override
  1255. //-----------------------------------------------------------------------------
  1256. VOID WINAPI XMemFree( PVOID pAddress, DWORD dwAllocAttributes )
  1257. {
  1258. if ( !pAddress )
  1259. {
  1260. return;
  1261. }
  1262. XALLOC_ATTRIBUTES *pAttribs = (XALLOC_ATTRIBUTES *)&dwAllocAttributes;
  1263. bool bPhysical = ( pAttribs->dwMemoryType == XALLOC_MEMTYPE_PHYSICAL );
  1264. if ( !bPhysical && !pAttribs->dwHeapTracksAttributes && pAttribs->dwAllocatorId != eXALLOCAllocatorId_XUI )
  1265. {
  1266. switch ( pAttribs->dwAlignment )
  1267. {
  1268. case XALLOC_ALIGNMENT_4:
  1269. #if !defined(USE_LIGHT_MEM_DEBUG) && !defined(USE_MEM_DEBUG)
  1270. return g_pMemAlloc->Free( pAddress );
  1271. #else
  1272. return g_pMemAlloc->Free( pAddress, ::g_pszModule, 0 );
  1273. #endif
  1274. default:
  1275. return MemAlloc_FreeAligned( pAddress );
  1276. }
  1277. return;
  1278. }
  1279. XMemAlloc_RegisterDeallocation( pAddress, dwAllocAttributes );
  1280. XMemFreeDefault( pAddress, dwAllocAttributes );
  1281. }
  1282. //-----------------------------------------------------------------------------
  1283. // XMemSize
  1284. //
  1285. // XBox Memory Allocator Override
  1286. //-----------------------------------------------------------------------------
  1287. SIZE_T WINAPI XMemSize( PVOID pAddress, DWORD dwAllocAttributes )
  1288. {
  1289. XALLOC_ATTRIBUTES *pAttribs = (XALLOC_ATTRIBUTES *)&dwAllocAttributes;
  1290. bool bPhysical = ( pAttribs->dwMemoryType == XALLOC_MEMTYPE_PHYSICAL );
  1291. if ( !bPhysical && !pAttribs->dwHeapTracksAttributes && pAttribs->dwAllocatorId != eXALLOCAllocatorId_XUI )
  1292. {
  1293. switch ( pAttribs->dwAlignment )
  1294. {
  1295. case XALLOC_ALIGNMENT_4:
  1296. return g_pMemAlloc->GetSize( pAddress );
  1297. default:
  1298. return MemAlloc_GetSizeAligned( pAddress );
  1299. }
  1300. }
  1301. return XMemSizeDefault( pAddress, dwAllocAttributes );
  1302. }
  1303. #endif // _X360
  1304. #define MAX_LANG_LEN 64 /* max language name length */
  1305. #define MAX_CTRY_LEN 64 /* max country name length */
  1306. #define MAX_MODIFIER_LEN 0 /* max modifier name length - n/a */
  1307. #define MAX_LC_LEN (MAX_LANG_LEN+MAX_CTRY_LEN+MAX_MODIFIER_LEN+3)
  1308. #if _MSC_VER >= 1700 // VS 11 (VS 2012)
  1309. // Copied from C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src\mtdll.h
  1310. #ifndef _SETLOC_STRUCT_DEFINED
  1311. struct _is_ctype_compatible {
  1312. unsigned long id;
  1313. int is_clike;
  1314. };
  1315. typedef struct setloc_struct {
  1316. /* getqloc static variables */
  1317. wchar_t *pchLanguage;
  1318. wchar_t *pchCountry;
  1319. int iLocState;
  1320. int iPrimaryLen;
  1321. BOOL bAbbrevLanguage;
  1322. BOOL bAbbrevCountry;
  1323. UINT _cachecp;
  1324. wchar_t _cachein[MAX_LC_LEN];
  1325. wchar_t _cacheout[MAX_LC_LEN];
  1326. /* _setlocale_set_cat (LC_CTYPE) static variable */
  1327. struct _is_ctype_compatible _Loc_c[5];
  1328. wchar_t _cacheLocaleName[LOCALE_NAME_MAX_LENGTH];
  1329. } _setloc_struct, *_psetloc_struct;
  1330. #define _SETLOC_STRUCT_DEFINED
  1331. #endif /* _SETLOC_STRUCT_DEFINED */
  1332. _CRTIMP extern unsigned long __cdecl __threadid(void);
  1333. #define _threadid (__threadid())
  1334. _CRTIMP extern uintptr_t __cdecl __threadhandle(void);
  1335. #define _threadhandle (__threadhandle())
  1336. /* Structure for each thread's data */
  1337. struct _tiddata {
  1338. unsigned long _tid; /* thread ID */
  1339. uintptr_t _thandle; /* thread handle */
  1340. int _terrno; /* errno value */
  1341. unsigned long _tdoserrno; /* _doserrno value */
  1342. unsigned int _fpds; /* Floating Point data segment */
  1343. unsigned long _holdrand; /* rand() seed value */
  1344. char * _token; /* ptr to strtok() token */
  1345. wchar_t * _wtoken; /* ptr to wcstok() token */
  1346. unsigned char * _mtoken; /* ptr to _mbstok() token */
  1347. /* following pointers get malloc'd at runtime */
  1348. char * _errmsg; /* ptr to strerror()/_strerror() buff */
  1349. wchar_t * _werrmsg; /* ptr to _wcserror()/__wcserror() buff */
  1350. char * _namebuf0; /* ptr to tmpnam() buffer */
  1351. wchar_t * _wnamebuf0; /* ptr to _wtmpnam() buffer */
  1352. char * _namebuf1; /* ptr to tmpfile() buffer */
  1353. wchar_t * _wnamebuf1; /* ptr to _wtmpfile() buffer */
  1354. char * _asctimebuf; /* ptr to asctime() buffer */
  1355. wchar_t * _wasctimebuf; /* ptr to _wasctime() buffer */
  1356. void * _gmtimebuf; /* ptr to gmtime() structure */
  1357. char * _cvtbuf; /* ptr to ecvt()/fcvt buffer */
  1358. unsigned char _con_ch_buf[MB_LEN_MAX];
  1359. /* ptr to putch() buffer */
  1360. unsigned short _ch_buf_used; /* if the _con_ch_buf is used */
  1361. /* following fields are needed by _beginthread code */
  1362. void * _initaddr; /* initial user thread address */
  1363. void * _initarg; /* initial user thread argument */
  1364. /* following three fields are needed to support signal handling and
  1365. * runtime errors */
  1366. void * _pxcptacttab; /* ptr to exception-action table */
  1367. void * _tpxcptinfoptrs; /* ptr to exception info pointers */
  1368. int _tfpecode; /* float point exception code */
  1369. #if ( defined( _MSC_VER ) && _MSC_VER >= 1900)
  1370. void *ptmbcinfo_dummy;
  1371. void *ptlocinfo_dummy;
  1372. #else
  1373. /* pointer to the copy of the multibyte character information used by
  1374. * the thread */
  1375. pthreadmbcinfo ptmbcinfo;
  1376. /* pointer to the copy of the locale informaton used by the thead */
  1377. pthreadlocinfo ptlocinfo;
  1378. #endif
  1379. int _ownlocale; /* if 1, this thread owns its own locale */
  1380. /* following field is needed by NLG routines */
  1381. unsigned long _NLG_dwCode;
  1382. /*
  1383. * Per-Thread data needed by C++ Exception Handling
  1384. */
  1385. void * _terminate; /* terminate() routine */
  1386. void * _unexpected; /* unexpected() routine */
  1387. void * _translator; /* S.E. translator */
  1388. void * _purecall; /* called when pure virtual happens */
  1389. void * _curexception; /* current exception */
  1390. void * _curcontext; /* current exception context */
  1391. int _ProcessingThrow; /* for uncaught_exception */
  1392. void * _curexcspec; /* for handling exceptions thrown from std::unexpected */
  1393. #if defined (_M_X64) || defined (_M_ARM)
  1394. void * _pExitContext;
  1395. void * _pUnwindContext;
  1396. void * _pFrameInfoChain;
  1397. #if defined (_WIN64)
  1398. unsigned __int64 _ImageBase;
  1399. unsigned __int64 _ThrowImageBase;
  1400. #else /* defined (_WIN64) */
  1401. unsigned __int32 _ImageBase;
  1402. unsigned __int32 _ThrowImageBase;
  1403. #endif /* defined (_WIN64) */
  1404. void * _pForeignException;
  1405. #elif defined (_M_IX86)
  1406. void * _pFrameInfoChain;
  1407. #endif /* defined (_M_IX86) */
  1408. _setloc_struct _setloc_data;
  1409. void * _reserved1; /* nothing */
  1410. void * _reserved2; /* nothing */
  1411. void * _reserved3; /* nothing */
  1412. #ifdef _M_IX86
  1413. void * _reserved4; /* nothing */
  1414. void * _reserved5; /* nothing */
  1415. #endif /* _M_IX86 */
  1416. int _cxxReThrow; /* Set to True if it's a rethrown C++ Exception */
  1417. unsigned long __initDomain; /* initial domain used by _beginthread[ex] for managed function */
  1418. };
  1419. #else // _MSC_VER >= 1700 // VS 11 (VS 2012)
  1420. struct _is_ctype_compatible {
  1421. unsigned long id;
  1422. int is_clike;
  1423. };
  1424. typedef struct setloc_struct {
  1425. /* getqloc static variables */
  1426. char *pchLanguage;
  1427. char *pchCountry;
  1428. int iLcidState;
  1429. int iPrimaryLen;
  1430. BOOL bAbbrevLanguage;
  1431. BOOL bAbbrevCountry;
  1432. LCID lcidLanguage;
  1433. LCID lcidCountry;
  1434. /* expand_locale static variables */
  1435. LC_ID _cacheid;
  1436. UINT _cachecp;
  1437. char _cachein[MAX_LC_LEN];
  1438. char _cacheout[MAX_LC_LEN];
  1439. /* _setlocale_set_cat (LC_CTYPE) static variable */
  1440. struct _is_ctype_compatible _Lcid_c[5];
  1441. } _setloc_struct, *_psetloc_struct;
  1442. struct _tiddata {
  1443. unsigned long _tid; /* thread ID */
  1444. uintptr_t _thandle; /* thread handle */
  1445. int _terrno; /* errno value */
  1446. unsigned long _tdoserrno; /* _doserrno value */
  1447. unsigned int _fpds; /* Floating Point data segment */
  1448. unsigned long _holdrand; /* rand() seed value */
  1449. char * _token; /* ptr to strtok() token */
  1450. wchar_t * _wtoken; /* ptr to wcstok() token */
  1451. unsigned char * _mtoken; /* ptr to _mbstok() token */
  1452. /* following pointers get malloc'd at runtime */
  1453. char * _errmsg; /* ptr to strerror()/_strerror() buff */
  1454. wchar_t * _werrmsg; /* ptr to _wcserror()/__wcserror() buff */
  1455. char * _namebuf0; /* ptr to tmpnam() buffer */
  1456. wchar_t * _wnamebuf0; /* ptr to _wtmpnam() buffer */
  1457. char * _namebuf1; /* ptr to tmpfile() buffer */
  1458. wchar_t * _wnamebuf1; /* ptr to _wtmpfile() buffer */
  1459. char * _asctimebuf; /* ptr to asctime() buffer */
  1460. wchar_t * _wasctimebuf; /* ptr to _wasctime() buffer */
  1461. void * _gmtimebuf; /* ptr to gmtime() structure */
  1462. char * _cvtbuf; /* ptr to ecvt()/fcvt buffer */
  1463. unsigned char _con_ch_buf[MB_LEN_MAX];
  1464. /* ptr to putch() buffer */
  1465. unsigned short _ch_buf_used; /* if the _con_ch_buf is used */
  1466. /* following fields are needed by _beginthread code */
  1467. void * _initaddr; /* initial user thread address */
  1468. void * _initarg; /* initial user thread argument */
  1469. /* following three fields are needed to support signal handling and
  1470. * runtime errors */
  1471. void * _pxcptacttab; /* ptr to exception-action table */
  1472. void * _tpxcptinfoptrs; /* ptr to exception info pointers */
  1473. int _tfpecode; /* float point exception code */
  1474. /* pointer to the copy of the multibyte character information used by
  1475. * the thread */
  1476. pthreadmbcinfo ptmbcinfo;
  1477. /* pointer to the copy of the locale informaton used by the thead */
  1478. pthreadlocinfo ptlocinfo;
  1479. int _ownlocale; /* if 1, this thread owns its own locale */
  1480. /* following field is needed by NLG routines */
  1481. unsigned long _NLG_dwCode;
  1482. /*
  1483. * Per-Thread data needed by C++ Exception Handling
  1484. */
  1485. void * _terminate; /* terminate() routine */
  1486. void * _unexpected; /* unexpected() routine */
  1487. void * _translator; /* S.E. translator */
  1488. void * _purecall; /* called when pure virtual happens */
  1489. void * _curexception; /* current exception */
  1490. void * _curcontext; /* current exception context */
  1491. int _ProcessingThrow; /* for uncaught_exception */
  1492. void * _curexcspec; /* for handling exceptions thrown from std::unexpected */
  1493. #if defined (_M_IA64) || defined (_M_AMD64)
  1494. void * _pExitContext;
  1495. void * _pUnwindContext;
  1496. void * _pFrameInfoChain;
  1497. unsigned __int64 _ImageBase;
  1498. #if defined (_M_IA64)
  1499. unsigned __int64 _TargetGp;
  1500. #endif /* defined (_M_IA64) */
  1501. unsigned __int64 _ThrowImageBase;
  1502. void * _pForeignException;
  1503. #elif defined (_M_IX86)
  1504. void * _pFrameInfoChain;
  1505. #endif /* defined (_M_IX86) */
  1506. _setloc_struct _setloc_data;
  1507. void * _encode_ptr; /* EncodePointer() routine */
  1508. void * _decode_ptr; /* DecodePointer() routine */
  1509. void * _reserved1; /* nothing */
  1510. void * _reserved2; /* nothing */
  1511. void * _reserved3; /* nothing */
  1512. int _cxxReThrow; /* Set to True if it's a rethrown C++ Exception */
  1513. unsigned long __initDomain; /* initial domain used by _beginthread[ex] for managed function */
  1514. };
  1515. #endif // _MSC_VER >= 1700 // VS 11 (VS 2012)
  1516. typedef struct _tiddata * _ptiddata;
  1517. #if (defined( _MSC_VER ) && _MSC_VER >= 1900)
  1518. //Do we need anything in here?
  1519. #else
  1520. class _LocaleUpdate
  1521. {
  1522. _locale_tstruct localeinfo;
  1523. _ptiddata ptd;
  1524. bool updated;
  1525. public:
  1526. _LocaleUpdate(_locale_t plocinfo)
  1527. : updated(false)
  1528. {
  1529. /*
  1530. if (plocinfo == NULL)
  1531. {
  1532. ptd = _getptd();
  1533. localeinfo.locinfo = ptd->ptlocinfo;
  1534. localeinfo.mbcinfo = ptd->ptmbcinfo;
  1535. __UPDATE_LOCALE(ptd, localeinfo.locinfo);
  1536. __UPDATE_MBCP(ptd, localeinfo.mbcinfo);
  1537. if (!(ptd->_ownlocale & _PER_THREAD_LOCALE_BIT))
  1538. {
  1539. ptd->_ownlocale |= _PER_THREAD_LOCALE_BIT;
  1540. updated = true;
  1541. }
  1542. }
  1543. else
  1544. {
  1545. localeinfo=*plocinfo;
  1546. }
  1547. */
  1548. }
  1549. ~_LocaleUpdate()
  1550. {
  1551. // if (updated)
  1552. // ptd->_ownlocale = ptd->_ownlocale & ~_PER_THREAD_LOCALE_BIT;
  1553. }
  1554. _locale_t GetLocaleT()
  1555. {
  1556. return &localeinfo;
  1557. }
  1558. };
  1559. #endif //_MSC_VER
  1560. #pragma warning(push)
  1561. #pragma warning(disable: 4483)
  1562. #if _MSC_FULL_VER >= 140050415
  1563. #define _NATIVE_STARTUP_NAMESPACE __identifier("<CrtImplementationDetails>")
  1564. #else /* _MSC_FULL_VER >= 140050415 */
  1565. #define _NATIVE_STARTUP_NAMESPACE __CrtImplementationDetails
  1566. #endif /* _MSC_FULL_VER >= 140050415 */
  1567. namespace _NATIVE_STARTUP_NAMESPACE
  1568. {
  1569. class NativeDll
  1570. {
  1571. private:
  1572. static const unsigned int ProcessDetach = 0;
  1573. static const unsigned int ProcessAttach = 1;
  1574. static const unsigned int ThreadAttach = 2;
  1575. static const unsigned int ThreadDetach = 3;
  1576. static const unsigned int ProcessVerifier = 4;
  1577. public:
  1578. inline static bool IsInDllMain()
  1579. {
  1580. return false;
  1581. }
  1582. inline static bool IsInProcessAttach()
  1583. {
  1584. return false;
  1585. }
  1586. inline static bool IsInProcessDetach()
  1587. {
  1588. return false;
  1589. }
  1590. inline static bool IsInVcclrit()
  1591. {
  1592. return false;
  1593. }
  1594. inline static bool IsSafeForManagedCode()
  1595. {
  1596. if (!IsInDllMain())
  1597. {
  1598. return true;
  1599. }
  1600. if (IsInVcclrit())
  1601. {
  1602. return true;
  1603. }
  1604. return !IsInProcessAttach() && !IsInProcessDetach();
  1605. }
  1606. };
  1607. }
  1608. #pragma warning(pop)
  1609. #endif // _MSC_VER >= 1400
  1610. #endif // !STEAM && !NO_MALLOC_OVERRIDE
  1611. #endif // _WIN32
  1612. #endif // _PS3