diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /utils/tfstats/memdbg.cpp | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'utils/tfstats/memdbg.cpp')
| -rw-r--r-- | utils/tfstats/memdbg.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/utils/tfstats/memdbg.cpp b/utils/tfstats/memdbg.cpp new file mode 100644 index 0000000..670090c --- /dev/null +++ b/utils/tfstats/memdbg.cpp @@ -0,0 +1,122 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: code to track allocations via replacing the global new operator +// some of this code was written by Paul Andre LeBlanc +// <[email protected]> I got it off of dejanews.com +// usage: new TRACKED object-type +// +// $Workfile: $ +// $Date: $ +// +//------------------------------------------------------------------------------------------------------ +// $Log: $ +// +// $NoKeywords: $ +//=============================================================================// +#include <new.h> +#include <stdio.h> +#include <stdlib.h> +#include <iostream.h> + + +#ifdef _DEBUG +#ifdef _MEMDEBUG +#define _MDEBUG +#endif +#endif + +#ifdef _MDEBUG +static int numBytesAllocated=0; +//these were written by me, wes cumberland, not paul andre leblanc +void * operator new(size_t size) +{ + void *ptr = malloc(size); + numBytesAllocated+=size; + return ptr; +} + +void * operator new[](size_t size) +{ + void *ptr = malloc(size); + numBytesAllocated+=size; + return ptr; +} + +void operator delete(void* ptr) +{ + free(ptr); +} + +void operator delete[](void* ptr) +{ + free(ptr); +} + + +//this code will track allocations +//this code was written by Paul Andre LeBlanc <[email protected]> +//I got it off of dejanews.com +void *operator new(size_t size, const char *file, const int line) +{ + void *ptr = new char[size]; + numBytesAllocated+=size; + cout << "new: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)"<< endl; + return ptr; +} + +void *operator new[](size_t size, const char *file, const int line) { + void *ptr = new char[size]; + numBytesAllocated+=size; + cout << "new[]: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)" << endl; + return ptr; +} + +void operator delete(void *ptr, const char *file, const int line) { + cout << "delete: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl; + delete [] (char *) ptr; +} + +void operator delete[](void *ptr, const char *file, const int line) +{ + cout << "delete[]: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl; + delete [] (char *) ptr; +} + +#endif +//------------------------------------------------------------------------------------------------------ +// Function: TFStats_win32_new_handler +// Purpose: this function will be called if TFStats runs out of memory (unlikely) +// this is a win32 specific version, the linux version does not pass an argument +// Input: sz - the size of the allocation that failed +// Output: int +//------------------------------------------------------------------------------------------------------ +int TFStats_win32_new_handler(size_t sz) +{ + printf("TFStats ran out of memory trying to allocate %li bytes\n",sz); + return 0; +} +//------------------------------------------------------------------------------------------------------ +// Function: TFStats_linux_new_handler +// Purpose: this function will be called if TFStats runs out of memory (unlikely) +// this is a linux specific version, the win32 version passes an argument +//------------------------------------------------------------------------------------------------------ +void TFStats_linux_new_handler(void) +{ + printf("TFStats ran out of memory!\n"); +} + + +//------------------------------------------------------------------------------------------------------ +// Function: TFStats_setNewHandler +// Purpose: sets the new handler to the TFStats new handler +//------------------------------------------------------------------------------------------------------ +void TFStats_setNewHandler() +{ +#ifdef WIN32 + _set_new_handler(TFStats_win32_new_handler); + _set_new_mode(1); +#else + std::set_new_handler(TFStats_linux_new_handler); + //std::set_new_mode(1); +#endif +} |