[llvm] r205677 - Simplify compression API by compressing into a SmallVector rather than a MemoryBuffer

David Blaikie dblaikie at gmail.com
Sat Apr 5 14:53:05 PDT 2014


Author: dblaikie
Date: Sat Apr  5 16:53:04 2014
New Revision: 205677

URL: http://llvm.org/viewvc/llvm-project?rev=205677&view=rev
Log:
Simplify compression API by compressing into a SmallVector rather than a MemoryBuffer

This is the other half of r205676.

Modified:
    llvm/trunk/include/llvm/Support/Compression.h
    llvm/trunk/lib/MC/MCAssembler.cpp
    llvm/trunk/lib/Support/Compression.cpp
    llvm/trunk/unittests/Support/CompressionTest.cpp

Modified: llvm/trunk/include/llvm/Support/Compression.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Compression.h?rev=205677&r1=205676&r2=205677&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Compression.h (original)
+++ llvm/trunk/include/llvm/Support/Compression.h Sat Apr  5 16:53:04 2014
@@ -20,7 +20,6 @@
 
 namespace llvm {
 
-class MemoryBuffer;
 class StringRef;
 
 namespace zlib {
@@ -43,8 +42,7 @@ enum Status {
 
 bool isAvailable();
 
-Status compress(StringRef InputBuffer,
-                std::unique_ptr<MemoryBuffer> &CompressedBuffer,
+Status compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
                 CompressionLevel Level = DefaultCompression);
 
 Status uncompress(StringRef InputBuffer,

Modified: llvm/trunk/lib/MC/MCAssembler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAssembler.cpp?rev=205677&r1=205676&r2=205677&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAssembler.cpp (original)
+++ llvm/trunk/lib/MC/MCAssembler.cpp Sat Apr  5 16:53:04 2014
@@ -237,23 +237,23 @@ const SmallVectorImpl<char> &MCCompresse
   assert(getParent()->size() == 1 &&
          "Only compress sections containing a single fragment");
   if (CompressedContents.empty()) {
-    std::unique_ptr<MemoryBuffer> CompressedSection;
+    // FIXME: could be more efficient if we let zlib::compress append to a
+    // buffer rather than always from the start.
     zlib::Status Success =
         zlib::compress(StringRef(getContents().data(), getContents().size()),
-                       CompressedSection);
+                       CompressedContents);
     (void)Success;
     assert(Success == zlib::StatusOK);
-    CompressedContents.push_back('Z');
-    CompressedContents.push_back('L');
-    CompressedContents.push_back('I');
-    CompressedContents.push_back('B');
+    static const StringRef Magic = "ZLIB";
     uint64_t Size = getContents().size();
     if (sys::IsLittleEndianHost)
       Size = sys::SwapByteOrder(Size);
-    CompressedContents.append(reinterpret_cast<char *>(&Size),
-                              reinterpret_cast<char *>(&Size + 1));
-    CompressedContents.append(CompressedSection->getBuffer().begin(),
-                              CompressedSection->getBuffer().end());
+    CompressedContents.insert(CompressedContents.begin(),
+                              Magic.size() + sizeof(Size));
+    std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
+    std::copy(reinterpret_cast<char *>(&Size),
+              reinterpret_cast<char *>(&Size + 1),
+              CompressedContents.begin() + Magic.size());
   }
   return CompressedContents;
 }

Modified: llvm/trunk/lib/Support/Compression.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Compression.cpp?rev=205677&r1=205676&r2=205677&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Compression.cpp (original)
+++ llvm/trunk/lib/Support/Compression.cpp Sat Apr  5 16:53:04 2014
@@ -16,7 +16,6 @@
 #include "llvm/Config/config.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/MemoryBuffer.h"
 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
 #include <zlib.h>
 #endif
@@ -47,20 +46,15 @@ static zlib::Status encodeZlibReturnValu
 
 bool zlib::isAvailable() { return true; }
 zlib::Status zlib::compress(StringRef InputBuffer,
-                            std::unique_ptr<MemoryBuffer> &CompressedBuffer,
+                            SmallVectorImpl<char> &CompressedBuffer,
                             CompressionLevel Level) {
   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
-  std::unique_ptr<char[]> TmpBuffer(new char[CompressedSize]);
+  CompressedBuffer.resize(CompressedSize);
   int CLevel = encodeZlibCompressionLevel(Level);
   Status Res = encodeZlibReturnValue(::compress2(
-      (Bytef *)TmpBuffer.get(), &CompressedSize,
+      (Bytef *)CompressedBuffer.data(), &CompressedSize,
       (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
-  if (Res == StatusOK) {
-    CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
-        StringRef(TmpBuffer.get(), CompressedSize)));
-    // Tell MSan that memory initialized by zlib is valid.
-    __msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize);
-  }
+  CompressedBuffer.resize(CompressedSize);
   return Res;
 }
 
@@ -82,12 +76,12 @@ uint32_t zlib::crc32(StringRef Buffer) {
 #else
 bool zlib::isAvailable() { return false; }
 zlib::Status zlib::compress(StringRef InputBuffer,
-                            std::unique_ptr<MemoryBuffer> &CompressedBuffer,
+                            SmallVectorImpl<char> &CompressedBuffer,
                             CompressionLevel Level) {
   return zlib::StatusUnsupported;
 }
 zlib::Status zlib::uncompress(StringRef InputBuffer,
-                              std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
+                              SmallVectorImpl<char> &UncompressedBuffer,
                               size_t UncompressedSize) {
   return zlib::StatusUnsupported;
 }

Modified: llvm/trunk/unittests/Support/CompressionTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CompressionTest.cpp?rev=205677&r1=205676&r2=205677&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CompressionTest.cpp (original)
+++ llvm/trunk/unittests/Support/CompressionTest.cpp Sat Apr  5 16:53:04 2014
@@ -13,8 +13,8 @@
 
 #include "llvm/Support/Compression.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/Config/config.h"
-#include "llvm/Support/MemoryBuffer.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -24,19 +24,17 @@ namespace {
 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
 
 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
-  std::unique_ptr<MemoryBuffer> Compressed;
+  SmallString<32> Compressed;
   SmallString<32> Uncompressed;
   EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
   // Check that uncompressed buffer is the same as original.
-  EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
-                                             Uncompressed, Input.size()));
-  EXPECT_EQ(Input.size(), Uncompressed.size());
-  EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
+  EXPECT_EQ(zlib::StatusOK,
+            zlib::uncompress(Compressed, Uncompressed, Input.size()));
+  EXPECT_EQ(Input, Uncompressed);
   if (Input.size() > 0) {
     // Uncompression fails if expected length is too short.
     EXPECT_EQ(zlib::StatusBufferTooShort,
-              zlib::uncompress(Compressed->getBuffer(), Uncompressed,
-                               Input.size() - 1));
+              zlib::uncompress(Compressed, Uncompressed, Input.size() - 1));
   }
 }
 





More information about the llvm-commits mailing list