Add a sys::OwningMemoryBlock class, which is a sys::MemoryBlock that owns its
authorLang Hames <lhames@gmail.com>
Sat, 31 Oct 2015 00:55:32 +0000 (00:55 +0000)
committerLang Hames <lhames@gmail.com>
Sat, 31 Oct 2015 00:55:32 +0000 (00:55 +0000)
underlying memory, and will automatically release it on destruction.

Use this to tidy up the orc::IndirectStubsInfo class.

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

include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
include/llvm/Support/Memory.h
lib/ExecutionEngine/Orc/OrcTargetSupport.cpp

index 2637ea5..58273ae 100644 (file)
@@ -59,9 +59,16 @@ public:
     const static unsigned PtrSize = 8;
 
     IndirectStubsInfo() : NumStubs(0) {}
-    IndirectStubsInfo(IndirectStubsInfo&&);
-    IndirectStubsInfo& operator=(IndirectStubsInfo&&);
-    ~IndirectStubsInfo();
+    IndirectStubsInfo(IndirectStubsInfo &&Other)
+        : NumStubs(Other.NumStubs), StubsMem(std::move(Other.StubsMem)) {
+      Other.NumStubs = 0;
+    }
+    IndirectStubsInfo& operator=(IndirectStubsInfo &&Other) {
+      NumStubs = Other.NumStubs;
+      Other.NumStubs = 0;
+      StubsMem = std::move(Other.StubsMem);
+      return *this;
+    }
 
     /// @brief Number of stubs in this block.
     unsigned getNumStubs() const { return NumStubs; }
@@ -69,18 +76,19 @@ public:
     /// @brief Get a pointer to the stub at the given index, which must be in
     ///        the range 0 .. getNumStubs() - 1.
     void* getStub(unsigned Idx) const {
-      return static_cast<uint64_t*>(StubsBlock.base()) + Idx;
+      return static_cast<uint64_t*>(StubsMem.base()) + Idx;
     }
 
     /// @brief Get a pointer to the implementation-pointer at the given index,
     ///        which must be in the range 0 .. getNumStubs() - 1.
     void** getPtr(unsigned Idx) const {
-      return static_cast<void**>(PtrsBlock.base()) + Idx;
+      char *PtrsBase =
+        static_cast<char*>(StubsMem.base()) + NumStubs * StubSize;
+      return reinterpret_cast<void**>(PtrsBase) + Idx;
     }
   private:
     unsigned NumStubs;
-    sys::MemoryBlock StubsBlock;
-    sys::MemoryBlock PtrsBlock;
+    sys::OwningMemoryBlock StubsMem;
   };
 
   /// @brief Emit at least MinStubs worth of indirect call stubs, rounded out to
index 47fd327..8103aea 100644 (file)
@@ -155,6 +155,31 @@ namespace sys {
     /// as writable.
     static bool setRangeWritable(const void *Addr, size_t Size);
   };
+
+  /// Owning version of MemoryBlock.
+  class OwningMemoryBlock {
+  public:
+    OwningMemoryBlock() = default;
+    explicit OwningMemoryBlock(MemoryBlock M) : M(M) {}
+    OwningMemoryBlock(OwningMemoryBlock &&Other) {
+      M = Other.M;
+      Other.M = MemoryBlock();
+    }
+    OwningMemoryBlock& operator=(OwningMemoryBlock &&Other) {
+      M = Other.M;
+      Other.M = MemoryBlock();
+      return *this;
+    }
+    ~OwningMemoryBlock() {
+      Memory::releaseMappedMemory(M);
+    }
+    void *base() const { return M.base(); }
+    size_t size() const { return M.size(); }
+    MemoryBlock getMemoryBlock() const { return M; }
+  private:
+    MemoryBlock M;
+  };
+
 }
 }
 
index 1e2d58c..c03b935 100644 (file)
@@ -144,27 +144,6 @@ OrcX86_64::insertCompileCallbackTrampolines(Module &M,
   return GetLabelName;
 }
 
-OrcX86_64::IndirectStubsInfo::IndirectStubsInfo(IndirectStubsInfo &&Other) {
-  StubsBlock = std::move(Other.StubsBlock);
-  PtrsBlock = std::move(Other.PtrsBlock);
-  Other.StubsBlock = sys::MemoryBlock();
-  Other.PtrsBlock = sys::MemoryBlock();
-}
-
-OrcX86_64::IndirectStubsInfo&
-OrcX86_64::IndirectStubsInfo::operator=(IndirectStubsInfo &&Other) {
-  StubsBlock = std::move(Other.StubsBlock);
-  PtrsBlock = std::move(Other.PtrsBlock);
-  Other.StubsBlock = sys::MemoryBlock();
-  Other.PtrsBlock = sys::MemoryBlock();
-  return *this;
-}
-
-OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() {
-  sys::Memory::releaseMappedMemory(StubsBlock);
-  sys::Memory::releaseMappedMemory(PtrsBlock);
-}
-
 std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
                                                   unsigned MinStubs,
                                                   void *InitialPtrVal) {
@@ -197,19 +176,20 @@ std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
 
   // Allocate memory for stubs and pointers in one call.
   std::error_code EC;
-  auto InitialBlock = sys::Memory::allocateMappedMemory(2 * NumPages * PageSize,
-                                                        nullptr,
-                                                        sys::Memory::MF_READ |
-                                                        sys::Memory::MF_WRITE,
-                                                        EC);
+  auto StubsMem =
+    sys::OwningMemoryBlock(
+      sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, nullptr,
+                                        sys::Memory::MF_READ |
+                                        sys::Memory::MF_WRITE,
+                                        EC));
 
   if (EC)
     return EC;
 
   // Create separate MemoryBlocks representing the stubs and pointers.
-  sys::MemoryBlock StubsBlock(InitialBlock.base(), NumPages * PageSize);
-  sys::MemoryBlock PtrsBlock(static_cast<char*>(InitialBlock.base()) +
-                             NumPages * PageSize,
+  sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize);
+  sys::MemoryBlock PtrsBlock(static_cast<char*>(StubsMem.base()) +
+                               NumPages * PageSize,
                              NumPages * PageSize);
 
   // Populate the stubs page stubs and mark it executable.
@@ -230,8 +210,7 @@ std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
     Ptr[I] = InitialPtrVal;
 
   StubsInfo.NumStubs = NumStubs;
-  StubsInfo.StubsBlock = std::move(StubsBlock);
-  StubsInfo.PtrsBlock = std::move(PtrsBlock);
+  StubsInfo.StubsMem = std::move(StubsMem);
 
   return std::error_code();
 }