platformMemory.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platformMemory.h"
  23. #include "console/dynamicTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "console/console.h"
  28. #include "platform/profiler.h"
  29. #include "platform/threads/mutex.h"
  30. #include "core/module.h"
  31. #ifdef _WIN32
  32. #include <windows.h>
  33. #include <dbghelp.h>
  34. #pragma comment(lib, "Dbghelp.lib")
  35. #else
  36. #include <execinfo.h>
  37. #endif
  38. // If profile paths are enabled, disable profiling of the
  39. // memory manager as that would cause a cyclic dependency
  40. // through the string table's allocation stuff used by the
  41. // profiler (talk about a long sentence...)
  42. #ifdef TORQUE_ENABLE_PROFILE_PATH
  43. # undef PROFILE_START
  44. # undef PROFILE_END
  45. # undef PROFILE_SCOPE
  46. # define PROFILE_START( x )
  47. # define PROFILE_END()
  48. # define PROFILE_SCOPE( x )
  49. #endif
  50. #ifdef TORQUE_MULTITHREAD
  51. void* gMemMutex = NULL;
  52. #endif
  53. //-------------------------------------- Make sure we don't have the define set
  54. #ifdef new
  55. #undef new
  56. #endif
  57. //---------------------------------------------------------------------------
  58. namespace Memory
  59. {
  60. #if !defined(TORQUE_DISABLE_MEMORY_MANAGER)
  61. static const U32 MaxAllocs = 10240;
  62. static MemInfo allocList[MaxAllocs];
  63. static U32 allocCount = 0;
  64. static U32 currentAllocId = 0;
  65. static bool initialized = false;
  66. char gLogFilename[256] = "memlog.txt";
  67. bool gStackTrace = false;
  68. void init()
  69. {
  70. if (initialized) return;
  71. std::memset(allocList, 0, sizeof(allocList));
  72. allocCount = 0;
  73. currentAllocId = 0;
  74. initialized = true;
  75. }
  76. void shutdown()
  77. {
  78. if (!initialized) return;
  79. FILE* log = std::fopen(gLogFilename, "w");
  80. if (!log)
  81. return;
  82. std::fprintf(log, "\n--- Memory Leak Report ---\n");
  83. for (U32 i = 0; i < allocCount; ++i)
  84. {
  85. if (allocList[i].ptr != nullptr)
  86. {
  87. std::fprintf(log, "Leak: %p (%zu bytes) from %s:%u [id=%u]\n",
  88. allocList[i].ptr, allocList[i].size,
  89. allocList[i].file ? allocList[i].file : "(null)", allocList[i].line,
  90. allocList[i].allocId);
  91. if (gStackTrace)
  92. {
  93. #ifdef _WIN32
  94. SYMBOL_INFO* symbol = (SYMBOL_INFO*)malloc(sizeof(SYMBOL_INFO) + 256);
  95. symbol->MaxNameLen = 255;
  96. symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
  97. HANDLE process = GetCurrentProcess();
  98. SymInitialize(process, NULL, TRUE);
  99. for (int j = 0; j < allocList[i].backtraceSize; ++j)
  100. {
  101. DWORD64 addr = (DWORD64)(allocList[i].backtracePtrs[j]);
  102. if (SymFromAddr(process, addr, 0, symbol))
  103. {
  104. std::fprintf(log, " [%d] %s - 0x%0llX\n", j, symbol->Name, symbol->Address);
  105. }
  106. else
  107. {
  108. std::fprintf(log, " [%d] ??? - 0x%0llX\n", j, addr);
  109. }
  110. }
  111. std::free(symbol);
  112. #else
  113. char** symbols = backtrace_symbols(allocList[i].backtracePtrs, allocList[i].backtraceSize);
  114. for (int j = 0; j < allocList[i].backtraceSize; ++j)
  115. {
  116. std::fprintf(log, " [%d] %s\n", j, symbols[j]);
  117. }
  118. std::free(symbols);
  119. #endif
  120. }
  121. }
  122. }
  123. std::fclose(log);
  124. initialized = false;
  125. }
  126. void checkPtr(void* ptr)
  127. {
  128. for (U32 i = 0; i < allocCount; ++i)
  129. if (allocList[i].ptr == ptr)
  130. return;
  131. Platform::debugBreak();
  132. }
  133. static void* alloc(dsize_t size, bool array, const char* fileName, U32 line)
  134. {
  135. if (size == 0)
  136. return nullptr;
  137. void* ptr = std::malloc(size);
  138. if (!ptr)
  139. return nullptr;
  140. if (!initialized || allocCount >= MaxAllocs)
  141. return ptr;
  142. MemInfo& info = allocList[allocCount++];
  143. info.ptr = ptr;
  144. info.size = size;
  145. info.file = fileName ? fileName : "unknown";
  146. info.line = line;
  147. info.allocId = currentAllocId++;
  148. info.flagged = false;
  149. if (gStackTrace)
  150. {
  151. #ifdef _WIN32
  152. info.backtraceSize = CaptureStackBackTrace(0, 16, info.backtracePtrs, nullptr);
  153. #else
  154. info.backtraceSize = backtrace(info.backtracePtrs, MaxBacktraceDepth);
  155. #endif
  156. }
  157. return ptr;
  158. }
  159. static void free(void* ptr, bool array)
  160. {
  161. if (!ptr || !initialized)
  162. return;
  163. for (U32 i = 0; i < allocCount; ++i)
  164. {
  165. if (allocList[i].ptr == ptr)
  166. {
  167. std::free(ptr);
  168. allocList[i] = allocList[allocCount - 1];
  169. allocList[--allocCount] = {};
  170. return;
  171. }
  172. }
  173. // Unknown pointer, still free it.
  174. std::free(ptr);
  175. }
  176. void getMemoryInfo(void* ptr, MemInfo& info)
  177. {
  178. if (!ptr || !initialized)
  179. return;
  180. for (U32 i = 0; i < allocCount; ++i)
  181. {
  182. if (allocList[i].ptr == ptr)
  183. {
  184. info = allocList[i];
  185. return;
  186. }
  187. }
  188. }
  189. static void* realloc(void* oldPtr, dsize_t newSize, const char* fileName, U32 line)
  190. {
  191. if (!initialized)
  192. return std::realloc(oldPtr, newSize); // fallback if not tracking
  193. if (newSize == 0)
  194. {
  195. free(oldPtr, false);
  196. return nullptr;
  197. }
  198. if (oldPtr == nullptr)
  199. return alloc(newSize, false, fileName, line);
  200. void* newPtr = std::realloc(oldPtr, newSize);
  201. if (!newPtr)
  202. return nullptr;
  203. // Update existing record
  204. for (U32 i = 0; i < allocCount; ++i)
  205. {
  206. if (allocList[i].ptr == oldPtr)
  207. {
  208. allocList[i].ptr = newPtr;
  209. allocList[i].size = newSize;
  210. allocList[i].file = fileName;
  211. allocList[i].line = line;
  212. allocList[i].allocId = currentAllocId++;
  213. return newPtr;
  214. }
  215. }
  216. // Not found — see if newPtr is already being tracked
  217. for (U32 i = 0; i < allocCount; ++i)
  218. {
  219. if (allocList[i].ptr == newPtr)
  220. {
  221. allocList[i].size = newSize;
  222. allocList[i].file = fileName;
  223. allocList[i].line = line;
  224. allocList[i].allocId = currentAllocId++;
  225. return newPtr;
  226. }
  227. }
  228. // Still not found — treat as a new allocation
  229. if (allocCount < MaxAllocs)
  230. {
  231. MemInfo& info = allocList[allocCount++];
  232. info = {};
  233. info.ptr = newPtr;
  234. info.size = newSize;
  235. info.file = fileName;
  236. info.line = line;
  237. info.allocId = currentAllocId++;
  238. }
  239. return newPtr;
  240. }
  241. #endif
  242. }
  243. //---------------------------------------------------------------------------
  244. //---------------------------------------------------------------------------
  245. #if !defined(TORQUE_DISABLE_MEMORY_MANAGER)
  246. // Manage our own memory, add overloaded memory operators and functions
  247. void* FN_CDECL operator new(dsize_t size, const char* fileName, const U32 line)
  248. {
  249. return Memory::alloc(size, false, fileName, line);
  250. }
  251. void* FN_CDECL operator new[](dsize_t size, const char* fileName, const U32 line)
  252. {
  253. return Memory::alloc(size, true, fileName, line);
  254. }
  255. void* FN_CDECL operator new(dsize_t size)
  256. {
  257. return Memory::alloc(size, false, NULL, 0);
  258. }
  259. void* FN_CDECL operator new[](dsize_t size)
  260. {
  261. return Memory::alloc(size, true, NULL, 0);
  262. }
  263. void FN_CDECL operator delete(void* mem)
  264. {
  265. Memory::free(mem, false);
  266. }
  267. void FN_CDECL operator delete[](void* mem)
  268. {
  269. Memory::free(mem, true);
  270. }
  271. void* dMalloc_r(dsize_t in_size, const char* fileName, const dsize_t line)
  272. {
  273. return Memory::alloc(in_size, false, fileName, line);
  274. }
  275. void dFree(void* in_pFree)
  276. {
  277. Memory::free(in_pFree, false);
  278. }
  279. void* dRealloc_r(void* in_pResize, dsize_t in_size, const char* fileName, const dsize_t line)
  280. {
  281. return Memory::realloc(in_pResize, in_size, fileName, line);
  282. }
  283. #else
  284. // Don't manage our own memory
  285. void* dMalloc_r(dsize_t in_size, const char* fileName, const dsize_t line)
  286. {
  287. return malloc(in_size);
  288. }
  289. void dFree(void* in_pFree)
  290. {
  291. free(in_pFree);
  292. }
  293. void* dRealloc_r(void* in_pResize, dsize_t in_size, const char* fileName, const dsize_t line)
  294. {
  295. return realloc(in_pResize,in_size);
  296. }
  297. #endif