Team Fortress 2 Source Code as on 22/4/2020
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.

122 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: code to track allocations via replacing the global new operator
  4. // some of this code was written by Paul Andre LeBlanc
  5. // <[email protected]> I got it off of dejanews.com
  6. // usage: new TRACKED object-type
  7. //
  8. // $Workfile: $
  9. // $Date: $
  10. //
  11. //------------------------------------------------------------------------------------------------------
  12. // $Log: $
  13. //
  14. // $NoKeywords: $
  15. //=============================================================================//
  16. #include <new.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <iostream.h>
  20. #ifdef _DEBUG
  21. #ifdef _MEMDEBUG
  22. #define _MDEBUG
  23. #endif
  24. #endif
  25. #ifdef _MDEBUG
  26. static int numBytesAllocated=0;
  27. //these were written by me, wes cumberland, not paul andre leblanc
  28. void * operator new(size_t size)
  29. {
  30. void *ptr = malloc(size);
  31. numBytesAllocated+=size;
  32. return ptr;
  33. }
  34. void * operator new[](size_t size)
  35. {
  36. void *ptr = malloc(size);
  37. numBytesAllocated+=size;
  38. return ptr;
  39. }
  40. void operator delete(void* ptr)
  41. {
  42. free(ptr);
  43. }
  44. void operator delete[](void* ptr)
  45. {
  46. free(ptr);
  47. }
  48. //this code will track allocations
  49. //this code was written by Paul Andre LeBlanc <[email protected]>
  50. //I got it off of dejanews.com
  51. void *operator new(size_t size, const char *file, const int line)
  52. {
  53. void *ptr = new char[size];
  54. numBytesAllocated+=size;
  55. cout << "new: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)"<< endl;
  56. return ptr;
  57. }
  58. void *operator new[](size_t size, const char *file, const int line) {
  59. void *ptr = new char[size];
  60. numBytesAllocated+=size;
  61. cout << "new[]: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)" << endl;
  62. return ptr;
  63. }
  64. void operator delete(void *ptr, const char *file, const int line) {
  65. cout << "delete: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl;
  66. delete [] (char *) ptr;
  67. }
  68. void operator delete[](void *ptr, const char *file, const int line)
  69. {
  70. cout << "delete[]: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl;
  71. delete [] (char *) ptr;
  72. }
  73. #endif
  74. //------------------------------------------------------------------------------------------------------
  75. // Function: TFStats_win32_new_handler
  76. // Purpose: this function will be called if TFStats runs out of memory (unlikely)
  77. // this is a win32 specific version, the linux version does not pass an argument
  78. // Input: sz - the size of the allocation that failed
  79. // Output: int
  80. //------------------------------------------------------------------------------------------------------
  81. int TFStats_win32_new_handler(size_t sz)
  82. {
  83. printf("TFStats ran out of memory trying to allocate %li bytes\n",sz);
  84. return 0;
  85. }
  86. //------------------------------------------------------------------------------------------------------
  87. // Function: TFStats_linux_new_handler
  88. // Purpose: this function will be called if TFStats runs out of memory (unlikely)
  89. // this is a linux specific version, the win32 version passes an argument
  90. //------------------------------------------------------------------------------------------------------
  91. void TFStats_linux_new_handler(void)
  92. {
  93. printf("TFStats ran out of memory!\n");
  94. }
  95. //------------------------------------------------------------------------------------------------------
  96. // Function: TFStats_setNewHandler
  97. // Purpose: sets the new handler to the TFStats new handler
  98. //------------------------------------------------------------------------------------------------------
  99. void TFStats_setNewHandler()
  100. {
  101. #ifdef WIN32
  102. _set_new_handler(TFStats_win32_new_handler);
  103. _set_new_mode(1);
  104. #else
  105. std::set_new_handler(TFStats_linux_new_handler);
  106. //std::set_new_mode(1);
  107. #endif
  108. }