Add AHB heapwalk to SimpleShell mem -v output
authorMichael Moon <triffid.hunter@gmail.com>
Mon, 23 Dec 2013 07:05:40 +0000 (18:05 +1100)
committerMichael Moon <triffid.hunter@gmail.com>
Mon, 23 Dec 2013 07:05:40 +0000 (18:05 +1100)
src/libs/MemoryPool.cpp
src/libs/MemoryPool.h
src/modules/utils/simpleshell/SimpleShell.cpp

index 3125cad..88a218a 100644 (file)
@@ -1,6 +1,7 @@
 #include "MemoryPool.h"
 
 #include <mri.h>
+#include <cstdio>
 
 #define offset(x) (((uint8_t*) x) - ((uint8_t*) this->base))
 
@@ -177,21 +178,24 @@ void MemoryPool::dealloc(void* d)
     } while (q < (_poolregion*) (((uint8_t*) base) + size));
 }
 
-void MemoryPool::debug()
+void MemoryPool::debug(StreamOutput* str)
 {
-#ifdef MEMDEBUG
     _poolregion* p = (_poolregion*) base;
-    MDEBUG("Start: %p (%+d)\n", p, sbrk);
+    uint32_t tot = 0;
+    uint32_t free = 0;
+    str->printf("Start: %ub MemoryPool at %p\n", size, p);
     do {
-        MDEBUG("Region at %p (%+4d): %s, %d bytes\n", p, offset(p), (p->used?"used":"free"), p->next);
-        if (p->next > sbrk)
+        str->printf("\tChunk at %p (%+4d): %s, %lu bytes\n", p, offset(p), (p->used?"used":"free"), p->next);
+        tot += p->next;
+        if (p->used == 0)
+            free += p->next;
+        if ((offset(p) + p->next >= size) || (p->next <= sizeof(_poolregion)))
         {
-            MDEBUG("End\n");
+            str->printf("End: total %lub, free: %lub\n", tot, free);
             return;
         }
         p = (_poolregion*) (((uint8_t*) p) + p->next);
     } while (1);
-#endif
 }
 
 bool MemoryPool::has(void* p)
index 75e168d..07a58ac 100644 (file)
@@ -11,6 +11,8 @@
     #define MDEBUG(...) do {} while (0)
 #endif
 
+#include "StreamOutput.h"
+
 /*
  * with MUCH thanks to http://www.parashift.com/c++-faq-lite/memory-pools.html
  *
@@ -26,7 +28,7 @@ public:
     void* alloc(size_t);
     void  dealloc(void* p);
 
-    void  debug(void);
+    void  debug(StreamOutput*);
 
     bool  has(void*);
 
index bf7bb3d..f962d2b 100644 (file)
@@ -299,6 +299,11 @@ void SimpleShell::mem_command( string parameters, StreamOutput *stream)
     stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
 
     stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
+    if (verbose)
+    {
+        AHB0.debug(stream);
+        AHB1.debug(stream);
+    }
 }
 
 static uint32_t getDeviceType()