about summary refs log tree commit diff homepage
path: root/lib/Core/MemoryManager.cpp
diff options
context:
space:
mode:
authorCristian Cadar <cristic@cs.stanford.edu>2012-01-18 18:58:10 +0000
committerCristian Cadar <cristic@cs.stanford.edu>2012-01-18 18:58:10 +0000
commitd32d0df34ab754d4d3b27b287092e536f03a231c (patch)
tree7d76e832672acd1ba11e2b3696b751d3baeee68a /lib/Core/MemoryManager.cpp
parent5344817c3de946e0636f6f671749c464dc4c02f2 (diff)
downloadklee-d32d0df34ab754d4d3b27b287092e536f03a231c.tar.gz
Nice patch by Gang Hu, Heming Cui and Junfeng Yang fixing a memory
leak in KLEE.

From Gang Hu: "The memory leak is caused by two reasons.  First, the
MemoryObject objects are not freed, until the MemoryManager is
destroyed.  Second, when KLEE allocates a non-fixed MemoryObject
object, KLEE also allocates a block of memory which is the same as the
object's size. This block of memory is never freed.  So, this patch
generally does reference counting on the MemoryObject objects, and
frees them as soon as the reference count drops to zero."

Many thanks to Paul Marinescu as well, who tested this patch
thoroughly on the Coreutils benchmarks.  On 1h runs, the memory
consumption typically goes down by 1-5%, but some applications which
see more significant gains.



git-svn-id: https://llvm.org/svn/llvm-project/klee/trunk@148402 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Core/MemoryManager.cpp')
-rw-r--r--lib/Core/MemoryManager.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/Core/MemoryManager.cpp b/lib/Core/MemoryManager.cpp
index 79fbcecf..06c234a2 100644
--- a/lib/Core/MemoryManager.cpp
+++ b/lib/Core/MemoryManager.cpp
@@ -25,8 +25,10 @@ using namespace klee;
 
 MemoryManager::~MemoryManager() { 
   while (!objects.empty()) {
-    MemoryObject *mo = objects.back();
-    objects.pop_back();
+    MemoryObject *mo = *objects.begin();
+    if (!mo->isFixed)
+      free((void *)mo->address);
+    objects.erase(mo);
     delete mo;
   }
 }
@@ -44,8 +46,8 @@ MemoryObject *MemoryManager::allocate(uint64_t size, bool isLocal,
   
   ++stats::allocations;
   MemoryObject *res = new MemoryObject(address, size, isLocal, isGlobal, false,
-                                       allocSite);
-  objects.push_back(res);
+                                       allocSite, this);
+  objects.insert(res);
   return res;
 }
 
@@ -62,11 +64,20 @@ MemoryObject *MemoryManager::allocateFixed(uint64_t address, uint64_t size,
 
   ++stats::allocations;
   MemoryObject *res = new MemoryObject(address, size, false, true, true,
-                                       allocSite);
-  objects.push_back(res);
+                                       allocSite, this);
+  objects.insert(res);
   return res;
 }
 
 void MemoryManager::deallocate(const MemoryObject *mo) {
   assert(0);
 }
+
+void MemoryManager::markFreed(MemoryObject *mo) {
+  if (objects.find(mo) != objects.end())
+  {
+    if (!mo->isFixed)
+      free((void *)mo->address);
+    objects.erase(mo);
+  }
+}