From: Lang Hames Date: Sat, 31 Oct 2015 00:55:32 +0000 (+0000) Subject: Add a sys::OwningMemoryBlock class, which is a sys::MemoryBlock that owns its X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=2cbdea79bd9ba472aeeaacb32b3b01b67f542624;p=oota-llvm.git 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251731 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h b/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h index 2637ea593d5..58273ae4616 100644 --- a/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h +++ b/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h @@ -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(StubsBlock.base()) + Idx; + return static_cast(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(PtrsBlock.base()) + Idx; + char *PtrsBase = + static_cast(StubsMem.base()) + NumStubs * StubSize; + return reinterpret_cast(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 diff --git a/include/llvm/Support/Memory.h b/include/llvm/Support/Memory.h index 47fd327e2e2..8103aea2fa2 100644 --- a/include/llvm/Support/Memory.h +++ b/include/llvm/Support/Memory.h @@ -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; + }; + } } diff --git a/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp b/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp index 1e2d58cd562..c03b935baeb 100644 --- a/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp +++ b/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp @@ -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(InitialBlock.base()) + - NumPages * PageSize, + sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize); + sys::MemoryBlock PtrsBlock(static_cast(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(); }