changeset 877:f7e5ee59ba17 am-dev

debug code to monitor large malloc/free (currently disabled)
author Alain Mazy <alain@mazy.be>
date Wed, 03 Jul 2019 10:16:22 +0200
parents 580dd82e13f5
children 8f7930f589ef
files Platforms/Wasm/Defaults.cpp
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Platforms/Wasm/Defaults.cpp	Wed Jul 03 10:15:29 2019 +0200
+++ b/Platforms/Wasm/Defaults.cpp	Wed Jul 03 10:16:22 2019 +0200
@@ -41,6 +41,39 @@
 extern "C" {
 #endif
 
+#if 0
+  size_t bigChunksTotalSize = 0;
+  std::map<void*, size_t> allocatedBigChunks;
+
+  extern void* emscripten_builtin_malloc(size_t bytes);
+  extern void emscripten_builtin_free(void* mem);
+
+  void * __attribute__((noinline)) malloc(size_t size)
+  {
+    void *ptr = emscripten_builtin_malloc(size);
+    if (size > 100000)
+    {
+      bigChunksTotalSize += size;
+      printf("++ Allocated %zu bytes, got %p. (%zu MB consumed by big chunks)\n", size, ptr, bigChunksTotalSize/(1024*1024));
+      allocatedBigChunks[ptr] = size;
+    }
+    return ptr;
+  }
+
+  void __attribute__((noinline)) free(void *ptr)
+  {
+    emscripten_builtin_free(ptr);
+
+    std::map<void*, size_t>::iterator it = allocatedBigChunks.find(ptr);
+    if (it != allocatedBigChunks.end())
+    {
+      bigChunksTotalSize -= it->second;
+      printf("--     Freed %zu bytes at %p.   (%zu MB consumed by big chunks)\n", it->second, ptr, bigChunksTotalSize/(1024*1024));
+      allocatedBigChunks.erase(it);
+    }
+  }
+#endif // 0
+
   using namespace OrthancStone;
 
   // when WASM needs a C++ viewport