BeefMem.cpp 901 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "gperftools/src/tcmalloc.cc"
  2. #include "BeefySysLib/Common.h"
  3. #ifdef BFMEM_DYNAMIC
  4. #define BFMEM_EXPORT __declspec(dllexport)
  5. #else
  6. #define BFMEM_EXPORT
  7. #endif
  8. USING_NS_BF;
  9. void PatchWindowsFunctions()
  10. {
  11. // For TCMalloc, don't really patch
  12. }
  13. namespace tcmalloc
  14. {
  15. extern "C" int RunningOnValgrind(void)
  16. {
  17. return 0;
  18. }
  19. }
  20. struct TCMallocRecord
  21. {
  22. void* mPtr;
  23. int mSize;
  24. };
  25. static Array<TCMallocRecord> gTCMallocRecords;
  26. void TCMalloc_RecordAlloc(void* ptr, int size)
  27. {
  28. TCMallocRecord mallocRecord = { ptr, size };
  29. gTCMallocRecords.push_back(mallocRecord);
  30. }
  31. static void TCMalloc_FreeAllocs()
  32. {
  33. for (auto& record : gTCMallocRecords)
  34. {
  35. ::VirtualFree(record.mPtr, 0, MEM_RELEASE);
  36. }
  37. gTCMallocRecords.Clear();
  38. }
  39. extern "C" BFMEM_EXPORT void* BfmAlloc(int size, int align)
  40. {
  41. return tc_malloc(size);
  42. }
  43. extern "C" BFMEM_EXPORT void BfmFree(void* ptr)
  44. {
  45. return tc_free(ptr);
  46. }