[lld] r355966 - ELF: Use bump pointer allocator for uncompressed section buffers. NFCI.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 13:32:31 PDT 2019


Author: pcc
Date: Tue Mar 12 13:32:30 2019
New Revision: 355966

URL: http://llvm.org/viewvc/llvm-project?rev=355966&view=rev
Log:
ELF: Use bump pointer allocator for uncompressed section buffers. NFCI.

This shaves another word off SectionBase and makes it possible to clone a
section using the implicit copy constructor.

This basically reverts r311056, which removed the mutex in order to
make the code easier to understand. On balance I think it's probably more
straightforward to have a mutex here than to have an unusual copy constructor
in SectionBase.

Differential Revision: https://reviews.llvm.org/D59269

Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=355966&r1=355965&r2=355966&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Tue Mar 12 13:32:30 2019
@@ -144,13 +144,18 @@ size_t InputSectionBase::getSize() const
 
 void InputSectionBase::uncompress() const {
   size_t Size = UncompressedSize;
-  UncompressedBuf.reset(new char[Size]);
+  char *UncompressedBuf;
+  {
+    static std::mutex Mu;
+    std::lock_guard<std::mutex> Lock(Mu);
+    UncompressedBuf = BAlloc.Allocate<char>(Size);
+  }
 
-  if (Error E =
-          zlib::uncompress(toStringRef(RawData), UncompressedBuf.get(), Size))
+  if (Error E = zlib::uncompress(toStringRef(RawData), UncompressedBuf, Size))
     fatal(toString(this) +
           ": uncompress failed: " + llvm::toString(std::move(E)));
-  RawData = makeArrayRef((uint8_t *)UncompressedBuf.get(), Size);
+  RawData = makeArrayRef((uint8_t *)UncompressedBuf, Size);
+  UncompressedSize = -1;
 }
 
 uint64_t InputSectionBase::getOffsetInFile() const {
@@ -1062,7 +1067,7 @@ template <class ELFT> void InputSection:
 
   // If this is a compressed section, uncompress section contents directly
   // to the buffer.
-  if (UncompressedSize >= 0 && !UncompressedBuf) {
+  if (UncompressedSize >= 0) {
     size_t Size = UncompressedSize;
     if (Error E = zlib::uncompress(toStringRef(RawData),
                                    (char *)(Buf + OutSecOff), Size))

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=355966&r1=355965&r2=355966&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Tue Mar 12 13:32:30 2019
@@ -133,7 +133,7 @@ public:
   }
 
   ArrayRef<uint8_t> data() const {
-    if (UncompressedSize >= 0 && !UncompressedBuf)
+    if (UncompressedSize >= 0)
       uncompress();
     return RawData;
   }
@@ -210,10 +210,11 @@ protected:
 
   mutable ArrayRef<uint8_t> RawData;
 
-  // A pointer that owns uncompressed data if a section is compressed by zlib.
-  // Since the feature is not used often, this is usually a nullptr.
-  mutable std::unique_ptr<char[]> UncompressedBuf;
-  int64_t UncompressedSize = -1;
+  // This field stores the uncompressed size of the compressed data in RawData,
+  // or -1 if RawData is not compressed (either because the section wasn't
+  // compressed in the first place, or because we ended up uncompressing it).
+  // Since the feature is not used often, this is usually -1.
+  mutable int64_t UncompressedSize = -1;
 };
 
 // SectionPiece represents a piece of splittable section contents.




More information about the llvm-commits mailing list