From b4074c010bc3d6c9a7323fabf68578b7b900a8a5 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Sat, 5 Apr 2014 21:53:04 +0000 Subject: [PATCH] Simplify compression API by compressing into a SmallVector rather than a MemoryBuffer This is the other half of r205676. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205677 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Compression.h | 4 +--- lib/MC/MCAssembler.cpp | 20 ++++++++++---------- lib/Support/Compression.cpp | 18 ++++++------------ unittests/Support/CompressionTest.cpp | 14 ++++++-------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/include/llvm/Support/Compression.h b/include/llvm/Support/Compression.h index 26298092866..8152b60eb09 100644 --- a/include/llvm/Support/Compression.h +++ b/include/llvm/Support/Compression.h @@ -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 &CompressedBuffer, +Status compress(StringRef InputBuffer, SmallVectorImpl &CompressedBuffer, CompressionLevel Level = DefaultCompression); Status uncompress(StringRef InputBuffer, diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 724ca292bef..ddb543715c7 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -237,23 +237,23 @@ const SmallVectorImpl &MCCompressedFragment::getCompressedContents() const assert(getParent()->size() == 1 && "Only compress sections containing a single fragment"); if (CompressedContents.empty()) { - std::unique_ptr 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(&Size), - reinterpret_cast(&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(&Size), + reinterpret_cast(&Size + 1), + CompressedContents.begin() + Magic.size()); } return CompressedContents; } diff --git a/lib/Support/Compression.cpp b/lib/Support/Compression.cpp index 329a402a070..c32eb21343e 100644 --- a/lib/Support/Compression.cpp +++ b/lib/Support/Compression.cpp @@ -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 #endif @@ -47,20 +46,15 @@ static zlib::Status encodeZlibReturnValue(int ReturnValue) { bool zlib::isAvailable() { return true; } zlib::Status zlib::compress(StringRef InputBuffer, - std::unique_ptr &CompressedBuffer, + SmallVectorImpl &CompressedBuffer, CompressionLevel Level) { unsigned long CompressedSize = ::compressBound(InputBuffer.size()); - std::unique_ptr 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 &CompressedBuffer, + SmallVectorImpl &CompressedBuffer, CompressionLevel Level) { return zlib::StatusUnsupported; } zlib::Status zlib::uncompress(StringRef InputBuffer, - std::unique_ptr &UncompressedBuffer, + SmallVectorImpl &UncompressedBuffer, size_t UncompressedSize) { return zlib::StatusUnsupported; } diff --git a/unittests/Support/CompressionTest.cpp b/unittests/Support/CompressionTest.cpp index 30df0509cce..698ae3aa2fc 100644 --- a/unittests/Support/CompressionTest.cpp +++ b/unittests/Support/CompressionTest.cpp @@ -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 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)); } } -- 2.34.1