Browse Source

Added memory debug dump (MSVC debug mode only.)

Lasse Öörni 13 years ago
parent
commit
d23803e285

+ 1 - 0
Docs/ScriptAPI.dox

@@ -4286,6 +4286,7 @@ Methods:<br>
 - void Exit()
 - void Exit()
 - void DumpProfilingData()
 - void DumpProfilingData()
 - void DumpResources()
 - void DumpResources()
+- void DumpMemory()
 - Console@ CreateConsole()
 - Console@ CreateConsole()
 - DebugHud@ CreateDebugHud()
 - DebugHud@ CreateDebugHud()
 
 

+ 2 - 0
Engine/Container/Allocator.cpp

@@ -25,6 +25,8 @@
 
 
 #include "stdio.h"
 #include "stdio.h"
 
 
+#include "DebugNew.h"
+
 AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSize, unsigned capacity)
 AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSize, unsigned capacity)
 {
 {
     if (!capacity)
     if (!capacity)

+ 0 - 0
Engine/Core/DebugNew.h → Engine/Container/DebugNew.h


+ 2 - 0
Engine/Container/RefCounted.cpp

@@ -25,6 +25,8 @@
 
 
 #include <cassert>
 #include <cassert>
 
 
+#include "DebugNew.h"
+
 RefCounted::RefCounted() :
 RefCounted::RefCounted() :
     refCount_(new RefCount())
     refCount_(new RefCount())
 {
 {

+ 2 - 0
Engine/Container/Str.cpp

@@ -26,6 +26,8 @@
 
 
 #include <cstdio>
 #include <cstdio>
 
 
+#include "DebugNew.h"
+
 char String::endZero = 0;
 char String::endZero = 0;
 
 
 String::String(const WString& str) :
 String::String(const WString& str) :

+ 55 - 0
Engine/Engine/Engine.cpp

@@ -49,6 +49,23 @@
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
 
 
+#if defined(_MSC_VER) && defined(_DEBUG)
+// From dbgint.h
+#define nNoMansLandSize 4
+
+typedef struct _CrtMemBlockHeader
+{
+    struct _CrtMemBlockHeader* pBlockHeaderNext;
+    struct _CrtMemBlockHeader* pBlockHeaderPrev;
+    char* szFileName;
+    int nLine;
+    size_t nDataSize;
+    int nBlockUse;
+    long lRequest;
+    unsigned char gap[nNoMansLandSize];
+} _CrtMemBlockHeader;
+#endif
+
 OBJECTTYPESTATIC(Engine);
 OBJECTTYPESTATIC(Engine);
 
 
 Engine::Engine(Context* context) :
 Engine::Engine(Context* context) :
@@ -406,6 +423,44 @@ void Engine::DumpResources()
     LOGRAW("Total memory use of all resources " + String(cache->GetTotalMemoryUse()) + "\n\n");
     LOGRAW("Total memory use of all resources " + String(cache->GetTotalMemoryUse()) + "\n\n");
 }
 }
 
 
+void Engine::DumpMemory()
+{
+    #if defined(_MSC_VER) && defined(_DEBUG)
+    _CrtMemState state;
+    _CrtMemCheckpoint(&state);
+    _CrtMemBlockHeader* block = state.pBlockHeader;
+    unsigned total = 0;
+    unsigned blocks = 0;
+    
+    for (;;)
+    {
+        if (block && block->pBlockHeaderNext)
+            block = block->pBlockHeaderNext;
+        else
+            break;
+    }
+    
+    while (block)
+    {
+        if (block->nBlockUse > 0)
+        {
+            if (block->szFileName)
+                LOGRAW("Block " + String((int)block->lRequest) + ": " + String(block->nDataSize) + " bytes, file " + String(block->szFileName) + " line " + String(block->nLine) + "\n");
+            else
+                LOGRAW("Block " + String((int)block->lRequest) + ": " + String(block->nDataSize) + " bytes\n");
+            
+            total += block->nDataSize;
+            ++blocks;
+        }
+        block = block->pBlockHeaderPrev;
+    }
+    
+    LOGRAW("Total allocated memory " + String(total) + " bytes in " + String(blocks) + " blocks\n\n");
+    #else
+    LOGRAW("DumpMemory() supported on MSVC debug mode only\n\n");
+    #endif
+}
+
 void Engine::Update()
 void Engine::Update()
 {
 {
     PROFILE(Update);
     PROFILE(Update);

+ 2 - 0
Engine/Engine/Engine.h

@@ -65,6 +65,8 @@ public:
     void DumpProfilingData();
     void DumpProfilingData();
     /// Dump information of all resources to the log.
     /// Dump information of all resources to the log.
     void DumpResources();
     void DumpResources();
+    /// Dump information of all memory allocations to the log. Supported in MSVC debug mode only.
+    void DumpMemory();
     
     
     /// Return the minimum frames per second.
     /// Return the minimum frames per second.
     int GetMinFps() const { return minFps_; }
     int GetMinFps() const { return minFps_; }

+ 1 - 0
Engine/Engine/EngineAPI.cpp

@@ -96,6 +96,7 @@ static void RegisterEngine(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Engine", "void Exit()", asMETHOD(Engine, Exit), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void Exit()", asMETHOD(Engine, Exit), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void DumpProfilingData()", asMETHOD(Engine, DumpProfilingData), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void DumpProfilingData()", asMETHOD(Engine, DumpProfilingData), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void DumpResources()", asMETHOD(Engine, DumpResources), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void DumpResources()", asMETHOD(Engine, DumpResources), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Engine", "void DumpMemory()", asMETHOD(Engine, DumpMemory), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "Console@+ CreateConsole()", asMETHOD(Engine, CreateConsole), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "Console@+ CreateConsole()", asMETHOD(Engine, CreateConsole), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "DebugHud@+ CreateDebugHud()", asMETHOD(Engine, CreateDebugHud), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "DebugHud@+ CreateDebugHud()", asMETHOD(Engine, CreateDebugHud), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void set_minFps(int)", asMETHOD(Engine, SetMinFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void set_minFps(int)", asMETHOD(Engine, SetMinFps), asCALL_THISCALL);