[llvm] r251731 - Add a sys::OwningMemoryBlock class, which is a sys::MemoryBlock that owns its

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 30 17:55:33 PDT 2015


Author: lhames
Date: Fri Oct 30 19:55:32 2015
New Revision: 251731

URL: http://llvm.org/viewvc/llvm-project?rev=251731&view=rev
Log:
Add a sys::OwningMemoryBlock class, which is a sys::MemoryBlock that owns its
underlying memory, and will automatically release it on destruction.

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

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

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h?rev=251731&r1=251730&r2=251731&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h Fri Oct 30 19:55:32 2015
@@ -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

Modified: llvm/trunk/include/llvm/Support/Memory.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Memory.h?rev=251731&r1=251730&r2=251731&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Memory.h (original)
+++ llvm/trunk/include/llvm/Support/Memory.h Fri Oct 30 19:55:32 2015
@@ -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;
+  };
+
 }
 }
 

Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp?rev=251731&r1=251730&r2=251731&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp Fri Oct 30 19:55:32 2015
@@ -144,27 +144,6 @@ OrcX86_64::insertCompileCallbackTrampoli
   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::emitIndirectS
 
   // 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::emitIndirectS
     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();
 }




More information about the llvm-commits mailing list