AllocDebug.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. //#define BP_ALLOC_TRACK
  3. #include <cstdint>
  4. #ifdef BF_PLATFORM_WINDOWS
  5. #define _CRTDBG_MAP_ALLOC
  6. #include <stdlib.h>
  7. #include <crtdbg.h>
  8. //#define USE_BF_ALLOCDEBUG
  9. #ifdef USE_BF_ALLOCDEBUG
  10. #define DBG_NEW new ( __FILE__ , __LINE__ )
  11. #define new DBG_NEW
  12. #undef delete
  13. #pragma push_macro("new")
  14. #undef new
  15. #undef delete
  16. void* operator new(std::size_t size);
  17. void* operator new(std::size_t size, const char* fileName, int lineNum);
  18. void operator delete(void* ptr, const char* fileName, int lineNum);
  19. void operator delete[](void* ptr, const char* fileName, int lineNum);
  20. void operator delete(void* ptr);
  21. void operator delete[](void* ptr);
  22. #pragma pop_macro("new")
  23. /*#undef delete
  24. #define delete DbgHeapDeleter(__FILE__, __LINE__) <<
  25. void DbgHeapFree(const void* ptr, const char* fileName, int lineNum);
  26. struct DbgHeapDeleter
  27. {
  28. const char* mFileName;
  29. int mLineNum;
  30. DbgHeapDeleter(const char* fileName, int lineNum)
  31. {
  32. mFileName = fileName;
  33. mLineNum = lineNum;
  34. }
  35. void operator<<(const void* ptr)
  36. {
  37. DbgHeapFree(ptr, mFileName, mLineNum);
  38. }
  39. };*/
  40. extern int gDbgHeapTransactionIdx;
  41. void DbgHeapCheck();
  42. void DbgHeapCheckLeaks();
  43. #elif (defined _WIN32) && (!defined BF_MINGW)//USE_BF_ALLOCDEBUG
  44. #ifndef DBG_NEW
  45. #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
  46. #define new DBG_NEW
  47. #endif
  48. #define DbgHeapCheck _CrtCheckMemory
  49. #define DbgHeapCheckLeaks _CrtDumpMemoryLeaks
  50. #endif // USE_BF_ALLOCDEBUG
  51. #endif // BF_PLATFORM_WINDOWS
  52. void BpAllocName(const char* str, int size);
  53. void BpDump();
  54. #ifdef BP_ALLOC_TRACK
  55. #define BP_ALLOC(str, size) BpAllocName(str, size);
  56. #define BP_ALLOC_RAW_T(T) BpAllocName(#T, sizeof(T))
  57. //#define BP_ALLOC_T(T) BpAllocName(#T, sizeof(T))
  58. #define BP_ALLOC_T(T)
  59. #else
  60. #define BP_ALLOC(str, size)
  61. #define BP_ALLOC_T(T)
  62. #define BP_ALLOC_RAW_T(T)
  63. #endif
  64. void* StompAlloc(int size);
  65. void StompFree(void* addr);
  66. template <typename T>
  67. class AllocatorStomp
  68. {
  69. public:
  70. T* allocate(intptr_t count)
  71. {
  72. return (T*)StompAlloc((int)(sizeof(T) * count));
  73. }
  74. void deallocate(T* ptr)
  75. {
  76. StompFree(ptr);
  77. }
  78. void* rawAllocate(intptr_t size)
  79. {
  80. return StompAlloc((int)size);
  81. }
  82. void rawDeallocate(void* ptr)
  83. {
  84. StompFree(ptr);
  85. }
  86. };