Fix PR3724 by searching for the largest free block when
authorChris Lattner <sabre@nondot.org>
Mon, 9 Mar 2009 21:34:10 +0000 (21:34 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 9 Mar 2009 21:34:10 +0000 (21:34 +0000)
allocating memory in the JIT.  This is insanely inefficient, but
hey, most people implement their own memory managers anyway.

Patch by Eric Yew!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66472 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/JIT/JITMemoryManager.cpp

index 0dcc71f837c6c526e16c85f498b069fc635550f0..b2bf8529d45667b6ffbed5a513b6f55ecc4ffd01 100644 (file)
@@ -278,10 +278,27 @@ namespace {
     /// startFunctionBody - When a function starts, allocate a block of free
     /// executable memory, returning a pointer to it and its actual size.
     unsigned char *startFunctionBody(const Function *F, uintptr_t &ActualSize) {
-      CurBlock = FreeMemoryList;
       
+      FreeRangeHeader* candidateBlock = FreeMemoryList;
+      FreeRangeHeader* head = FreeMemoryList;
+      FreeRangeHeader* iter = head->Next;
+
+      uintptr_t largest = candidateBlock->BlockSize;
+      
+      // Search for the largest free block
+      while (iter != head) {
+          if (iter->BlockSize > largest) {
+              largest = iter->BlockSize;
+              candidateBlock = iter;
+          }
+          iter = iter->Next;
+      }
+      
+      // Select this candidate block for allocation
+      CurBlock = candidateBlock;
+
       // Allocate the entire memory block.
-      FreeMemoryList = FreeMemoryList->AllocateBlock();
+      FreeMemoryList = candidateBlock->AllocateBlock();
       ActualSize = CurBlock->BlockSize-sizeof(MemoryRangeHeader);
       return (unsigned char *)(CurBlock+1);
     }