From 96a8ccfab758dc18a5ca4b65817b38616925ea00 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 3 Sep 2014 19:48:09 +0000 Subject: [PATCH] unique_ptrify a bunch of stuff through RuntimeDyld::loadObject git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217065 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ExecutionEngine/ObjectBuffer.h | 2 +- include/llvm/ExecutionEngine/ObjectImage.h | 2 +- include/llvm/ExecutionEngine/RuntimeDyld.h | 6 ++- lib/ExecutionEngine/MCJIT/MCJIT.cpp | 31 ++++++------- lib/ExecutionEngine/MCJIT/MCJIT.h | 3 +- .../RuntimeDyld/ObjectImageCommon.h | 13 +++--- .../RuntimeDyld/RuntimeDyld.cpp | 17 ++++--- .../RuntimeDyld/RuntimeDyldELF.cpp | 45 ++++++++++--------- .../RuntimeDyld/RuntimeDyldELF.h | 3 +- .../RuntimeDyld/RuntimeDyldMachO.h | 5 ++- tools/llvm-rtdyld/llvm-rtdyld.cpp | 12 ++--- 11 files changed, 71 insertions(+), 68 deletions(-) diff --git a/include/llvm/ExecutionEngine/ObjectBuffer.h b/include/llvm/ExecutionEngine/ObjectBuffer.h index 1950ad6deb9..ee4820aa6ee 100644 --- a/include/llvm/ExecutionEngine/ObjectBuffer.h +++ b/include/llvm/ExecutionEngine/ObjectBuffer.h @@ -29,7 +29,7 @@ class ObjectBuffer { virtual void anchor(); public: ObjectBuffer() {} - ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {} + ObjectBuffer(std::unique_ptr Buf) : Buffer(std::move(Buf)) {} virtual ~ObjectBuffer() {} MemoryBufferRef getMemBuffer() const { return Buffer->getMemBufferRef(); } diff --git a/include/llvm/ExecutionEngine/ObjectImage.h b/include/llvm/ExecutionEngine/ObjectImage.h index afd98c796af..dc142bd70af 100644 --- a/include/llvm/ExecutionEngine/ObjectImage.h +++ b/include/llvm/ExecutionEngine/ObjectImage.h @@ -31,7 +31,7 @@ protected: std::unique_ptr Buffer; public: - ObjectImage(ObjectBuffer *Input) : Buffer(Input) {} + ObjectImage(std::unique_ptr Input) : Buffer(std::move(Input)) {} virtual ~ObjectImage() {} virtual object::symbol_iterator begin_symbols() const = 0; diff --git a/include/llvm/ExecutionEngine/RuntimeDyld.h b/include/llvm/ExecutionEngine/RuntimeDyld.h index 65b06ffc407..2ca6421de66 100644 --- a/include/llvm/ExecutionEngine/RuntimeDyld.h +++ b/include/llvm/ExecutionEngine/RuntimeDyld.h @@ -53,13 +53,15 @@ public: /// Ownership of the input buffer is transferred to the ObjectImage /// instance returned from this function if successful. In the case of load /// failure, the input buffer will be deleted. - ObjectImage *loadObject(ObjectBuffer *InputBuffer); + std::unique_ptr + loadObject(std::unique_ptr InputBuffer); /// Prepare the referenced object file for execution. /// Ownership of the input object is transferred to the ObjectImage /// instance returned from this function if successful. In the case of load /// failure, the input object will be deleted. - ObjectImage *loadObject(std::unique_ptr InputObject); + std::unique_ptr + loadObject(std::unique_ptr InputObject); /// Get the address of our local copy of the symbol. This may or may not /// be the address used for relocation (clients can copy the data around diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 2724f8c61ef..f6c3c18a4fd 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -82,15 +82,9 @@ MCJIT::~MCJIT() { Dyld.deregisterEHFrames(); - LoadedObjectList::iterator it, end; - for (it = LoadedObjects.begin(), end = LoadedObjects.end(); it != end; ++it) { - ObjectImage *Obj = *it; - if (Obj) { + for (auto &Obj : LoadedObjects) + if (Obj) NotifyFreeingObject(*Obj); - delete Obj; - } - } - LoadedObjects.clear(); Archives.clear(); } @@ -106,11 +100,11 @@ bool MCJIT::removeModule(Module *M) { } void MCJIT::addObjectFile(std::unique_ptr Obj) { - ObjectImage *LoadedObject = Dyld.loadObject(std::move(Obj)); + std::unique_ptr LoadedObject = Dyld.loadObject(std::move(Obj)); if (!LoadedObject || Dyld.hasError()) report_fatal_error(Dyld.getErrorString()); - LoadedObjects.push_back(LoadedObject); + LoadedObjects.push_back(std::move(LoadedObject)); NotifyObjectEmitted(*LoadedObject); } @@ -183,9 +177,10 @@ void MCJIT::generateCodeForModule(Module *M) { std::unique_ptr ObjectToLoad; // Try to load the pre-compiled object from cache if possible if (ObjCache) { - std::unique_ptr PreCompiledObject(ObjCache->getObject(M)); - if (PreCompiledObject.get()) - ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.release())); + if (std::unique_ptr PreCompiledObject = + ObjCache->getObject(M)) + ObjectToLoad = + llvm::make_unique(std::move(PreCompiledObject)); } // If the cache did not contain a suitable object, compile the object @@ -196,8 +191,8 @@ void MCJIT::generateCodeForModule(Module *M) { // Load the object into the dynamic linker. // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). - ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.release()); - LoadedObjects.push_back(LoadedObject); + std::unique_ptr LoadedObject = + Dyld.loadObject(std::move(ObjectToLoad)); if (!LoadedObject) report_fatal_error(Dyld.getErrorString()); @@ -206,6 +201,8 @@ void MCJIT::generateCodeForModule(Module *M) { NotifyObjectEmitted(*LoadedObject); + LoadedObjects.push_back(std::move(LoadedObject)); + OwnedModules.markModuleAsLoaded(M); } @@ -563,10 +560,8 @@ void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { } void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { MutexGuard locked(lock); - for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { - JITEventListener *L = EventListeners[I]; + for (JITEventListener *L : EventListeners) L->NotifyFreeingObject(Obj); - } } uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) { diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.h b/lib/ExecutionEngine/MCJIT/MCJIT.h index 1c0d22e0ece..d52a7322eb3 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.h +++ b/lib/ExecutionEngine/MCJIT/MCJIT.h @@ -219,8 +219,7 @@ class MCJIT : public ExecutionEngine { SmallVector, 2> Archives; SmallVector, 2> Buffers; - typedef SmallVector LoadedObjectList; - LoadedObjectList LoadedObjects; + SmallVector, 2> LoadedObjects; // An optional ObjectCache to be notified of compiled objects and used to // perform lookup of pre-compiled code to avoid re-compilation. diff --git a/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h b/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h index ddf0e89b408..9bbf6a0d809 100644 --- a/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h +++ b/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h @@ -36,16 +36,13 @@ protected: // This form of the constructor allows subclasses to use // format-specific subclasses of ObjectFile directly - ObjectImageCommon(ObjectBuffer *Input, std::unique_ptr Obj) - : ObjectImage(Input), // saves Input as Buffer and takes ownership - ObjFile(std::move(Obj)) - { - } + ObjectImageCommon(std::unique_ptr Input, + std::unique_ptr Obj) + : ObjectImage(std::move(Input)), ObjFile(std::move(Obj)) {} public: - ObjectImageCommon(ObjectBuffer* Input) - : ObjectImage(Input) // saves Input as Buffer and takes ownership - { + ObjectImageCommon(std::unique_ptr Input) + : ObjectImage(std::move(Input)) { // FIXME: error checking? createObjectFile returns an ErrorOr // and should probably be checked for failure. MemoryBufferRef Buf = Buffer->getMemBuffer(); diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index d1b17ce94d8..1f326d8c478 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -794,7 +794,8 @@ createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM, return Dyld; } -ObjectImage *RuntimeDyld::loadObject(std::unique_ptr InputObject) { +std::unique_ptr +RuntimeDyld::loadObject(std::unique_ptr InputObject) { std::unique_ptr InputImage; ObjectFile &Obj = *InputObject; @@ -816,19 +817,21 @@ ObjectImage *RuntimeDyld::loadObject(std::unique_ptr InputObject) { report_fatal_error("Incompatible object format!"); Dyld->loadObject(InputImage.get()); - return InputImage.release(); + return InputImage; } -ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { +std::unique_ptr +RuntimeDyld::loadObject(std::unique_ptr InputBuffer) { std::unique_ptr InputImage; sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer()); + auto *InputBufferPtr = InputBuffer.get(); switch (Type) { case sys::fs::file_magic::elf_relocatable: case sys::fs::file_magic::elf_executable: case sys::fs::file_magic::elf_shared_object: case sys::fs::file_magic::elf_core: - InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer)); + InputImage = RuntimeDyldELF::createObjectImage(std::move(InputBuffer)); if (!Dyld) Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release(); break; @@ -842,7 +845,7 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { case sys::fs::file_magic::macho_bundle: case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: case sys::fs::file_magic::macho_dsym_companion: - InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer)); + InputImage = RuntimeDyldMachO::createObjectImage(std::move(InputBuffer)); if (!Dyld) Dyld = createRuntimeDyldMachO( static_cast(InputImage->getArch()), @@ -859,11 +862,11 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { report_fatal_error("Incompatible object format!"); } - if (!Dyld->isCompatibleFormat(InputBuffer)) + if (!Dyld->isCompatibleFormat(InputBufferPtr)) report_fatal_error("Incompatible object format!"); Dyld->loadObject(InputImage.get()); - return InputImage.release(); + return InputImage; } void *RuntimeDyld::getSymbolAddress(StringRef Name) { diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index adea5ad2241..803e5cb781c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -77,8 +77,10 @@ template class ELFObjectImage : public ObjectImageCommon { bool Registered; public: - ELFObjectImage(ObjectBuffer *Input, std::unique_ptr> Obj) - : ObjectImageCommon(Input, std::move(Obj)), Registered(false) {} + ELFObjectImage(std::unique_ptr Input, + std::unique_ptr> Obj) + : ObjectImageCommon(std::move(Input), std::move(Obj)), Registered(false) { + } virtual ~ELFObjectImage() { if (Registered) @@ -212,7 +214,8 @@ RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr Ob llvm_unreachable("Unexpected ELF format"); } -ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { +std::unique_ptr +RuntimeDyldELF::createObjectImage(std::unique_ptr Buffer) { if (Buffer->getBufferSize() < ELF::EI_NIDENT) llvm_unreachable("Unexpected ELF object size"); std::pair Ident = @@ -226,28 +229,30 @@ ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { auto Obj = llvm::make_unique>>( Buf, ec); - return new ELFObjectImage>( - Buffer, std::move(Obj)); - } else if (Ident.first == ELF::ELFCLASS32 && - Ident.second == ELF::ELFDATA2MSB) { + return llvm::make_unique< + ELFObjectImage>>(std::move(Buffer), + std::move(Obj)); + } + if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { auto Obj = llvm::make_unique>>(Buf, ec); - return new ELFObjectImage>(Buffer, - std::move(Obj)); - } else if (Ident.first == ELF::ELFCLASS64 && - Ident.second == ELF::ELFDATA2MSB) { + return llvm::make_unique>>( + std::move(Buffer), std::move(Obj)); + } + if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { auto Obj = llvm::make_unique>>( Buf, ec); - return new ELFObjectImage>(Buffer, std::move(Obj)); - } else if (Ident.first == ELF::ELFCLASS64 && - Ident.second == ELF::ELFDATA2LSB) { - auto Obj = - llvm::make_unique>>(Buf, - ec); - return new ELFObjectImage>(Buffer, std::move(Obj)); - } else - llvm_unreachable("Unexpected ELF format"); + return llvm::make_unique>>( + std::move(Buffer), std::move(Obj)); + } + assert(Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB && + "Unexpected ELF format"); + auto Obj = + llvm::make_unique>>(Buf, + ec); + return llvm::make_unique>>( + std::move(Buffer), std::move(Obj)); } RuntimeDyldELF::~RuntimeDyldELF() {} diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h index 1f2f90ef541..4aeab819179 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h @@ -119,7 +119,8 @@ public: ObjSectionToIDMap &SectionMap) override; virtual ~RuntimeDyldELF(); - static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer); + static std::unique_ptr + createObjectImage(std::unique_ptr InputBuffer); static ObjectImage *createObjectImageFromFile(std::unique_ptr Obj); }; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h index 8a00c0bdea0..bae7ca86e66 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -103,8 +103,9 @@ protected: public: /// Create an ObjectImage from the given ObjectBuffer. - static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) { - return new ObjectImageCommon(InputBuffer); + static std::unique_ptr + createObjectImage(std::unique_ptr InputBuffer) { + return llvm::make_unique(std::move(InputBuffer)); } /// Create an ObjectImage from the given ObjectFile. diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index 462f6c17145..e8e2e89f9ac 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -203,8 +203,8 @@ static int printLineInfoForInput() { std::unique_ptr LoadedObject; // Load the object file - LoadedObject.reset( - Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); + LoadedObject = Dyld.loadObject( + llvm::make_unique(std::move(*InputBuffer))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } @@ -264,8 +264,8 @@ static int executeInput() { return Error("unable to read input: '" + EC.message() + "'"); std::unique_ptr LoadedObject; // Load the object file - LoadedObject.reset( - Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); + LoadedObject = Dyld.loadObject( + llvm::make_unique(std::move(*InputBuffer))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } @@ -427,8 +427,8 @@ static int linkAndVerify() { std::unique_ptr LoadedObject; // Load the object file - LoadedObject.reset( - Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); + LoadedObject = Dyld.loadObject( + llvm::make_unique(std::move(*InputBuffer))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } -- 2.34.1