make sure to unlock keymgr if the JIT is created and destroyed, all
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITMemoryManager.cpp
index 248291634cc4c92cf30e28b02faba2ff63aaf68d..2819b6d4653bea7c17a7ffb2e249f6f80f465a0f 100644 (file)
@@ -18,6 +18,8 @@
 #include <map>
 #include <vector>
 #include <cassert>
+#include <climits>
+#include <cstdio>
 #include <cstdlib>
 #include <cstring>
 using namespace llvm;
@@ -47,7 +49,7 @@ namespace {
     
     /// BlockSize - This is the size in bytes of this memory block,
     /// including this header.
-    uintptr_t BlockSize : (sizeof(intptr_t)*8 - 2);
+    uintptr_t BlockSize : (sizeof(intptr_t)*CHAR_BIT - 2);
     
 
     /// getBlockAfter - Return the memory block immediately after this one.
@@ -257,6 +259,7 @@ namespace {
     
     unsigned char *CurStubPtr, *StubBase;
     unsigned char *GOTBase;      // Target Specific reserved memory
+    void *DlsymTable;            // Stub external symbol information
 
     // Centralize memory block allocation.
     sys::MemoryBlock getNewMemoryBlock(unsigned size);
@@ -268,17 +271,35 @@ namespace {
     ~DefaultJITMemoryManager();
 
     void AllocateGOT();
-
+    void SetDlsymTable(void *);
+    
     unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
                                 unsigned Alignment);
     
     /// 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);
     }
@@ -290,14 +311,31 @@ namespace {
       assert(FunctionEnd > FunctionStart);
       assert(FunctionStart == (unsigned char *)(CurBlock+1) &&
              "Mismatched function start/end!");
-      
+
       uintptr_t BlockSize = FunctionEnd - (unsigned char *)CurBlock;
       FunctionBlocks[F] = CurBlock;
 
       // Release the memory at the end of this block that isn't needed.
       FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
     }
-    
+
+    /// allocateSpace - Allocate a memory block of the given size.
+    unsigned char *allocateSpace(intptr_t Size, unsigned Alignment) {
+      CurBlock = FreeMemoryList;
+      FreeMemoryList = FreeMemoryList->AllocateBlock();
+
+      unsigned char *result = (unsigned char *)CurBlock+1;
+
+      if (Alignment == 0) Alignment = 1;
+      result = (unsigned char*)(((intptr_t)result+Alignment-1) &
+               ~(intptr_t)(Alignment-1));
+
+      uintptr_t BlockSize = result + Size - (unsigned char *)CurBlock;
+      FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
+
+      return result;
+    }
+
     /// startExceptionTable - Use startFunctionBody to allocate memory for the 
     /// function's exception table.
     unsigned char* startExceptionTable(const Function* F, 
@@ -325,6 +363,10 @@ namespace {
       return GOTBase;
     }
     
+    void *getDlsymTable() const {
+      return DlsymTable;
+    }
+    
     /// deallocateMemForFunction - Deallocate all memory for the specified
     /// function body.
     void deallocateMemForFunction(const Function *F) {
@@ -365,12 +407,31 @@ namespace {
       // Finally, remove this entry from TableBlocks.
       TableBlocks.erase(I);
     }
+
+    /// setMemoryWritable - When code generation is in progress,
+    /// the code pages may need permissions changed.
+    void setMemoryWritable(void)
+    {
+      for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
+        sys::Memory::setWritable(Blocks[i]);
+    }
+    /// setMemoryExecutable - When code generation is done and we're ready to
+    /// start execution, the code pages may need permissions changed.
+    void setMemoryExecutable(void)
+    {
+      for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
+        sys::Memory::setExecutable(Blocks[i]);
+    }
   };
 }
 
 DefaultJITMemoryManager::DefaultJITMemoryManager() {
   // Allocate a 16M block of memory for functions.
+#if defined(__APPLE__) && defined(__arm__)
+  sys::MemoryBlock MemBlock = getNewMemoryBlock(4 << 20);
+#else
   sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20);
+#endif
 
   unsigned char *MemBase = static_cast<unsigned char*>(MemBlock.base());
 
@@ -426,6 +487,7 @@ DefaultJITMemoryManager::DefaultJITMemoryManager() {
   FreeMemoryList = Mem0;
 
   GOTBase = NULL;
+  DlsymTable = NULL;
 }
 
 void DefaultJITMemoryManager::AllocateGOT() {
@@ -434,6 +496,9 @@ void DefaultJITMemoryManager::AllocateGOT() {
   HasGOT = true;
 }
 
+void DefaultJITMemoryManager::SetDlsymTable(void *ptr) {
+  DlsymTable = ptr;
+}
 
 DefaultJITMemoryManager::~DefaultJITMemoryManager() {
   for (unsigned i = 0, e = Blocks.size(); i != e; ++i)