llvm.org GIT mirror llvm / b206103
Simplify compression API by decompressing into a SmallVector rather than a MemoryBuffer This avoids an extra copy during decompression and avoids the use of MemoryBuffer which is a weirdly esoteric device that includes unrelated concepts like "file name" (its rather generic name is a bit misleading). Similar refactoring of zlib::compress coming up. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205676 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 6 years ago
5 changed file(s) with 18 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
1515
1616 #include "llvm/Support/DataTypes.h"
1717 #include
18 #include "llvm/ADT/SmallVector.h"
1819
1920 namespace llvm {
2021
4647 CompressionLevel Level = DefaultCompression);
4748
4849 Status uncompress(StringRef InputBuffer,
49 std::unique_ptrr> &UncompressedBuffer,
50 SmallVectorImplr> &UncompressedBuffer,
5051 size_t UncompressedSize);
5152
5253 uint32_t crc32(StringRef Buffer);
636636 if (!zlib::isAvailable() ||
637637 !consumeCompressedDebugSectionHeader(data, OriginalSize))
638638 continue;
639 std::unique_ptr UncompressedSection;
640 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
641 zlib::StatusOK)
639 UncompressedSections.resize(UncompressedSections.size() + 1);
640 if (zlib::uncompress(data, UncompressedSections.back(), OriginalSize) !=
641 zlib::StatusOK) {
642 UncompressedSections.pop_back();
642643 continue;
644 }
643645 // Make data point to uncompressed section contents and save its contents.
644646 name = name.substr(1);
645 data = UncompressedSection->getBuffer();
646 UncompressedSections.push_back(std::move(UncompressedSection));
647 data = UncompressedSections.back();
647648 }
648649
649650 StringRef *SectionData =
241241 StringRef RangeDWOSection;
242242 StringRef AddrSection;
243243
244 SmallVector<std::unique_ptr>, 4> UncompressedSections;
244 SmallVector<SmallString<32>, 4> UncompressedSections;
245245
246246 public:
247247 DWARFContextInMemory(object::ObjectFile *);
6464 }
6565
6666 zlib::Status zlib::uncompress(StringRef InputBuffer,
67 std::unique_ptrr> &UncompressedBuffer,
67 SmallVectorImplr> &UncompressedBuffer,
6868 size_t UncompressedSize) {
69 std::unique_ptr TmpBuffer(new char[UncompressedSize]);
70 Status Res = encodeZlibReturnValue(
71 ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize,
72 (const Bytef *)InputBuffer.data(), InputBuffer.size()));
73 if (Res == StatusOK) {
74 UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
75 StringRef(TmpBuffer.get(), UncompressedSize)));
76 // Tell MSan that memory initialized by zlib is valid.
77 __msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize);
78 }
69 UncompressedBuffer.resize(UncompressedSize);
70 Status Res = encodeZlibReturnValue(::uncompress(
71 (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize,
72 (const Bytef *)InputBuffer.data(), InputBuffer.size()));
73 UncompressedBuffer.resize(UncompressedSize);
7974 return Res;
8075 }
8176
2424
2525 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
2626 std::unique_ptr Compressed;
27 std::unique_ptr> Uncompressed;
27 SmallString<32> Uncompressed;
2828 EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
2929 // Check that uncompressed buffer is the same as original.
3030 EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
3131 Uncompressed, Input.size()));
32 EXPECT_EQ(Input.size(), Uncompressed->getBufferSize());
33 EXPECT_EQ(0,
34 memcmp(Input.data(), Uncompressed->getBufferStart(), Input.size()));
32 EXPECT_EQ(Input.size(), Uncompressed.size());
33 EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
3534 if (Input.size() > 0) {
3635 // Uncompression fails if expected length is too short.
3736 EXPECT_EQ(zlib::StatusBufferTooShort,