diff --git a/public/icvar.h b/public/icvar.h index 2682c5a6..a08083c9 100644 --- a/public/icvar.h +++ b/public/icvar.h @@ -12,7 +12,6 @@ #include "appframework/IAppSystem.h" #include "tier1/iconvar.h" -#include "tier0/memalloc.h" class ConCommandBase; class ConCommand; @@ -156,6 +155,7 @@ protected: class ICVarIteratorInternal { public: + virtual ~ICVarIteratorInternal() {} virtual void SetFirst( void ) = 0; virtual void Next( void ) = 0; virtual bool IsValid( void ) = 0; @@ -175,7 +175,7 @@ inline ICvar::Iterator::Iterator(ICvar *icvar) inline ICvar::Iterator::~Iterator( void ) { - g_pMemAlloc->Free(m_pIter); + delete m_pIter; } inline void ICvar::Iterator::SetFirst( void ) diff --git a/public/tier0/memalloc.h b/public/tier0/memalloc.h index 5fdf6214..6602f554 100644 --- a/public/tier0/memalloc.h +++ b/public/tier0/memalloc.h @@ -14,6 +14,14 @@ #pragma once #endif +#if !defined( NO_MALLOC_OVERRIDE ) && defined( POSIX ) +#define NO_MALLOC_OVERRIDE +#endif + +#if defined( NO_MALLOC_OVERRIDE ) && !defined( NO_HOOK_MALLOC ) +#define NO_HOOK_MALLOC +#endif + // Define this in release to get memory tracking even in release builds //#define USE_MEM_DEBUG 1 @@ -24,11 +32,15 @@ // Undefine this if using a compiler lacking threadsafe RTTI (like vc6) #define MEM_DEBUG_CLASSNAME 1 -#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE) - #include +#if defined( OSX ) +#include +#endif + #include "tier0/mem.h" +#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE) + struct _CrtMemState; #define MEMALLOC_VERSION 1 @@ -112,6 +124,13 @@ public: virtual void CompactIncremental() = 0; virtual void OutOfMemory( size_t nBytesAttempted = 0 ) = 0; + + // Region-based allocations + virtual void *RegionAlloc( int region, size_t nSize ) = 0; + virtual void *RegionAlloc( int region, size_t nSize, const char *pFileName, int nLine ) = 0; + + // Replacement for ::GlobalMemoryStatus which accounts for unused memory in our system + virtual void GlobalMemoryStatus( size_t *pUsedMemory, size_t *pFreeMemory ) = 0; }; //----------------------------------------------------------------------------- @@ -343,6 +362,45 @@ struct MemAllocFileLine_t //----------------------------------------------------------------------------- +#elif defined( POSIX ) + +#if defined( OSX ) +// Mac always aligns allocs, don't need to call posix_memalign which doesn't exist in 10.5.8 which TF2 still needs to run on +//inline void *memalign(size_t alignment, size_t size) {void *pTmp=NULL; posix_memalign(&pTmp, alignment, size); return pTmp;} +inline void *memalign(size_t alignment, size_t size) {void *pTmp=NULL; pTmp = malloc(size); return pTmp;} +#endif + +inline void *_aligned_malloc( size_t nSize, size_t align ) { return memalign( align, nSize ); } +inline void _aligned_free( void *ptr ) { free( ptr ); } + +inline void *MemAlloc_Alloc( size_t nSize, const char *pFileName = NULL, int nLine = 0 ) { return malloc( nSize ); } +inline void MemAlloc_Free( void *ptr, const char *pFileName = NULL, int nLine = 0 ) { free( ptr ); } + +inline void *MemAlloc_AllocAligned( size_t size, size_t align, const char *pszFile = NULL, int nLine = 0 ) { return memalign( align, size ); } +inline void *MemAlloc_AllocAlignedFileLine( size_t size, size_t align, const char *pszFile = NULL, int nLine = 0 ) { return memalign( align, size ); } +inline void MemAlloc_FreeAligned( void *pMemBlock, const char *pszFile = NULL, int nLine = 0 ) { free( pMemBlock ); } + +#if defined( OSX ) +inline size_t _msize( void *ptr ) { return malloc_size( ptr ); } +#else +inline size_t _msize( void *ptr ) { return malloc_usable_size( ptr ); } +#endif + +inline void *MemAlloc_ReallocAligned( void *ptr, size_t size, size_t align ) +{ + void *ptr_new_aligned = memalign( align, size ); + + if( ptr_new_aligned ) + { + size_t old_size = _msize( ptr ); + size_t copy_size = ( size < old_size ) ? size : old_size; + + memcpy( ptr_new_aligned, ptr, copy_size ); + free( ptr ); + } + + return ptr_new_aligned; +} #endif // !STEAM && !NO_MALLOC_OVERRIDE //----------------------------------------------------------------------------- @@ -351,9 +409,13 @@ struct MemAllocFileLine_t #define MEM_ALLOC_CREDIT_(tag) ((void)0) #define MEM_ALLOC_CREDIT() MEM_ALLOC_CREDIT_(__FILE__) +#define MEM_ALLOC_CREDIT_FUNCTION() #define MEM_ALLOC_CREDIT_CLASS() #define MEM_ALLOC_CLASSNAME(type) NULL +#define MemAlloc_PushAllocDbgInfo( pszFile, line ) +#define MemAlloc_PopAllocDbgInfo() +#define MEMALLOC_DEFINE_EXTERNAL_TRACKING( tag ) #endif // !STEAM && NO_MALLOC_OVERRIDE //----------------------------------------------------------------------------- diff --git a/public/tier0/memdbgon.h b/public/tier0/memdbgon.h index 9282efb7..b361d456 100644 --- a/public/tier0/memdbgon.h +++ b/public/tier0/memdbgon.h @@ -11,6 +11,14 @@ // to include this potentially multiple times (since we can deactivate debugging // by including memdbgoff.h) +#if !defined( NO_MALLOC_OVERRIDE ) && defined( POSIX ) +#define NO_MALLOC_OVERRIDE +#endif + +#if defined( NO_MALLOC_OVERRIDE ) && !defined( NO_HOOK_MALLOC ) +#define NO_HOOK_MALLOC +#endif + #if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE) // SPECIAL NOTE #2: This must be the final include in a .cpp or .h file!!! diff --git a/public/tier0/platform.h b/public/tier0/platform.h index 1fad3681..4ce42809 100644 --- a/public/tier0/platform.h +++ b/public/tier0/platform.h @@ -824,17 +824,7 @@ struct CPUInformation tchar* m_szProcessorID; // Processor vendor Identification. }; -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunknown-pragmas" -#pragma clang diagnostic ignored "-Wreturn-type-c-linkage" -#endif - -PLATFORM_INTERFACE const CPUInformation& GetCPUInformation(); - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif +DLL_GLOBAL_EXPORT const CPUInformation& GetCPUInformation(void); PLATFORM_INTERFACE void GetCurrentDate( int *pDay, int *pMonth, int *pYear ); diff --git a/tier1/generichash.cpp b/tier1/generichash.cpp index af784fc4..f89894f8 100644 --- a/tier1/generichash.cpp +++ b/tier1/generichash.cpp @@ -142,7 +142,7 @@ unsigned FASTCALL HashInt( const int n ) odd = g_nRandomValues[((n >> 8) & 0xff)]; even = g_nRandomValues[odd ^ (n >> 24)]; - odd = g_nRandomValues[even ^ (n >> 16) & 0xff]; + odd = g_nRandomValues[(even ^ (n >> 16)) & 0xff]; even = g_nRandomValues[odd ^ ((n >> 8) & 0xff)]; odd = g_nRandomValues[even ^ (n & 0xff)]; @@ -163,7 +163,7 @@ unsigned FASTCALL Hash4( const void *pKey ) odd = g_nRandomValues[((n >> 8) & 0xff)]; even = g_nRandomValues[odd ^ (n >> 24)]; - odd = g_nRandomValues[even ^ (n >> 16) & 0xff]; + odd = g_nRandomValues[(even ^ (n >> 16)) & 0xff]; even = g_nRandomValues[odd ^ ((n >> 8) & 0xff)]; odd = g_nRandomValues[even ^ (n & 0xff)]; @@ -185,7 +185,7 @@ unsigned FASTCALL Hash8( const void *pKey ) odd = g_nRandomValues[((n >> 8) & 0xff)]; even = g_nRandomValues[odd ^ (n >> 24)]; - odd = g_nRandomValues[even ^ (n >> 16) & 0xff]; + odd = g_nRandomValues[(even ^ (n >> 16)) & 0xff]; even = g_nRandomValues[odd ^ ((n >> 8) & 0xff)]; odd = g_nRandomValues[even ^ (n & 0xff)]; @@ -213,7 +213,7 @@ unsigned FASTCALL Hash12( const void *pKey ) odd = g_nRandomValues[((n >> 8) & 0xff)]; even = g_nRandomValues[odd ^ (n >> 24)]; - odd = g_nRandomValues[even ^ (n >> 16) & 0xff]; + odd = g_nRandomValues[(even ^ (n >> 16)) & 0xff]; even = g_nRandomValues[odd ^ ((n >> 8) & 0xff)]; odd = g_nRandomValues[even ^ (n & 0xff)]; @@ -247,7 +247,7 @@ unsigned FASTCALL Hash16( const void *pKey ) odd = g_nRandomValues[((n >> 8) & 0xff)]; even = g_nRandomValues[odd ^ (n >> 24)]; - odd = g_nRandomValues[even ^ (n >> 16) & 0xff]; + odd = g_nRandomValues[(even ^ (n >> 16)) & 0xff]; even = g_nRandomValues[odd ^ ((n >> 8) & 0xff)]; odd = g_nRandomValues[even ^ (n & 0xff)]; diff --git a/tier1/interface.cpp b/tier1/interface.cpp index 8252e570..93a68b18 100644 --- a/tier1/interface.cpp +++ b/tier1/interface.cpp @@ -241,7 +241,11 @@ CSysModule *Sys_LoadModule( const char *pModuleName ) if ( !Q_IsAbsolutePath( pModuleName ) ) { // full path wasn't passed in, using the current working dir - _getcwd( szCwd, sizeof( szCwd ) ); + if (!_getcwd( szCwd, sizeof( szCwd ) )) + { + Msg("Failed to load %s\n", pModuleName); + return NULL; + } if ( IsX360() ) { int i = CommandLine()->FindParm( "-basedir" );