llvm.org GIT mirror llvm / b4074c0
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 David Blaikie 5 years ago
4 changed file(s) with 23 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
1919
2020 namespace llvm {
2121
22 class MemoryBuffer;
2322 class StringRef;
2423
2524 namespace zlib {
4241
4342 bool isAvailable();
4443
45 Status compress(StringRef InputBuffer,
46 std::unique_ptrr> &CompressedBuffer,
44 Status compress(StringRef InputBuffer, SmallVectorImplr> &CompressedBuffer,
4745 CompressionLevel Level = DefaultCompression);
4846
4947 Status uncompress(StringRef InputBuffer,
236236 assert(getParent()->size() == 1 &&
237237 "Only compress sections containing a single fragment");
238238 if (CompressedContents.empty()) {
239 std::unique_ptr CompressedSection;
239 // FIXME: could be more efficient if we let zlib::compress append to a
240 // buffer rather than always from the start.
240241 zlib::Status Success =
241242 zlib::compress(StringRef(getContents().data(), getContents().size()),
242 CompressedSection);
243 CompressedContents);
243244 (void)Success;
244245 assert(Success == zlib::StatusOK);
245 CompressedContents.push_back('Z');
246 CompressedContents.push_back('L');
247 CompressedContents.push_back('I');
248 CompressedContents.push_back('B');
246 static const StringRef Magic = "ZLIB";
249247 uint64_t Size = getContents().size();
250248 if (sys::IsLittleEndianHost)
251249 Size = sys::SwapByteOrder(Size);
252 CompressedContents.append(reinterpret_cast(&Size),
253 reinterpret_cast(&Size + 1));
254 CompressedContents.append(CompressedSection->getBuffer().begin(),
255 CompressedSection->getBuffer().end());
250 CompressedContents.insert(CompressedContents.begin(),
251 Magic.size() + sizeof(Size));
252 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
253 std::copy(reinterpret_cast(&Size),
254 reinterpret_cast(&Size + 1),
255 CompressedContents.begin() + Magic.size());
256256 }
257257 return CompressedContents;
258258 }
1515 #include "llvm/Config/config.h"
1616 #include "llvm/Support/Compiler.h"
1717 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/MemoryBuffer.h"
1918 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
2019 #include
2120 #endif
4645
4746 bool zlib::isAvailable() { return true; }
4847 zlib::Status zlib::compress(StringRef InputBuffer,
49 std::unique_ptrr> &CompressedBuffer,
48 SmallVectorImplr> &CompressedBuffer,
5049 CompressionLevel Level) {
5150 unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52 std::unique_ptr TmpBuffer(new char[CompressedSize]);
51 CompressedBuffer.resize(CompressedSize);
5352 int CLevel = encodeZlibCompressionLevel(Level);
5453 Status Res = encodeZlibReturnValue(::compress2(
55 (Bytef *)TmpBuffer.get(), &CompressedSize,
54 (Bytef *)CompressedBuffer.data(), &CompressedSize,
5655 (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
57 if (Res == StatusOK) {
58 CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
59 StringRef(TmpBuffer.get(), CompressedSize)));
60 // Tell MSan that memory initialized by zlib is valid.
61 __msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize);
62 }
56 CompressedBuffer.resize(CompressedSize);
6357 return Res;
6458 }
6559
8175 #else
8276 bool zlib::isAvailable() { return false; }
8377 zlib::Status zlib::compress(StringRef InputBuffer,
84 std::unique_ptrr> &CompressedBuffer,
78 SmallVectorImplr> &CompressedBuffer,
8579 CompressionLevel Level) {
8680 return zlib::StatusUnsupported;
8781 }
8882 zlib::Status zlib::uncompress(StringRef InputBuffer,
89 std::unique_ptrr> &UncompressedBuffer,
83 SmallVectorImplr> &UncompressedBuffer,
9084 size_t UncompressedSize) {
9185 return zlib::StatusUnsupported;
9286 }
1212
1313 #include "llvm/Support/Compression.h"
1414 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/SmallString.h"
1516 #include "llvm/Config/config.h"
16 #include "llvm/Support/MemoryBuffer.h"
1717 #include "gtest/gtest.h"
1818
1919 using namespace llvm;
2323 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
2424
2525 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
26 std::unique_ptr> Compressed;
26 SmallString<32> Compressed;
2727 SmallString<32> Uncompressed;
2828 EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
2929 // Check that uncompressed buffer is the same as original.
30 EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
31 Uncompressed, Input.size()));
32 EXPECT_EQ(Input.size(), Uncompressed.size());
33 EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
30 EXPECT_EQ(zlib::StatusOK,
31 zlib::uncompress(Compressed, Uncompressed, Input.size()));
32 EXPECT_EQ(Input, Uncompressed);
3433 if (Input.size() > 0) {
3534 // Uncompression fails if expected length is too short.
3635 EXPECT_EQ(zlib::StatusBufferTooShort,
37 zlib::uncompress(Compressed->getBuffer(), Uncompressed,
38 Input.size() - 1));
36 zlib::uncompress(Compressed, Uncompressed, Input.size() - 1));
3937 }
4038 }
4139