[llvm] r373693 - [JITLink] Explicitly destroy bumpptr-allocated blocks to avoid a memory leak.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 3 22:24:41 PDT 2019


Author: lhames
Date: Thu Oct  3 22:24:40 2019
New Revision: 373693

URL: http://llvm.org/viewvc/llvm-project?rev=373693&view=rev
Log:
[JITLink] Explicitly destroy bumpptr-allocated blocks to avoid a memory leak.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/JITLink/JITLink.h
    llvm/trunk/lib/ExecutionEngine/JITLink/JITLink.cpp

Modified: llvm/trunk/include/llvm/ExecutionEngine/JITLink/JITLink.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/JITLink/JITLink.h?rev=373693&r1=373692&r2=373693&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/JITLink/JITLink.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/JITLink/JITLink.h Thu Oct  3 22:24:40 2019
@@ -633,10 +633,12 @@ private:
   template <typename... ArgTs> Block &createBlock(ArgTs &&... Args) {
     Block *B = reinterpret_cast<Block *>(Allocator.Allocate<Block>());
     new (B) Block(std::forward<ArgTs>(Args)...);
+    Blocks.insert(B);
     return *B;
   }
 
   void destroyBlock(Block &B) {
+    Blocks.erase(&B);
     B.~Block();
     Allocator.Deallocate(&B);
   }
@@ -707,6 +709,8 @@ public:
       : Name(std::move(Name)), PointerSize(PointerSize),
         Endianness(Endianness) {}
 
+  ~LinkGraph();
+
   /// Returns the name of this graph (usually the name of the original
   /// underlying MemoryBuffer).
   const std::string &getName() { return Name; }
@@ -728,19 +732,15 @@ public:
   Block &createContentBlock(Section &Parent, StringRef Content,
                             uint64_t Address, uint64_t Alignment,
                             uint64_t AlignmentOffset) {
-    auto &B = createBlock(Parent, Parent.getNextBlockOrdinal(), Content,
-                          Address, Alignment, AlignmentOffset);
-    Blocks.insert(&B);
-    return B;
+    return createBlock(Parent, Parent.getNextBlockOrdinal(), Content, Address,
+                       Alignment, AlignmentOffset);
   }
 
   /// Create a zero-fill block.
   Block &createZeroFillBlock(Section &Parent, uint64_t Size, uint64_t Address,
                              uint64_t Alignment, uint64_t AlignmentOffset) {
-    auto &B = createBlock(Parent, Parent.getNextBlockOrdinal(), Size, Address,
-                          Alignment, AlignmentOffset);
-    Blocks.insert(&B);
-    return B;
+    return createBlock(Parent, Parent.getNextBlockOrdinal(), Size, Address,
+                       Alignment, AlignmentOffset);
   }
 
   /// Add an external symbol.

Modified: llvm/trunk/lib/ExecutionEngine/JITLink/JITLink.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JITLink/JITLink.cpp?rev=373693&r1=373692&r2=373693&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JITLink/JITLink.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JITLink/JITLink.cpp Thu Oct  3 22:24:40 2019
@@ -145,6 +145,12 @@ Section::~Section() {
     Sym->~Symbol();
 }
 
+LinkGraph::~LinkGraph() {
+  // Destroy blocks.
+  for (auto *B : Blocks)
+    B->~Block();
+}
+
 void LinkGraph::dump(raw_ostream &OS,
                      std::function<StringRef(Edge::Kind)> EdgeKindToName) {
   if (!EdgeKindToName)




More information about the llvm-commits mailing list