mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-20 12:31:40 +00:00
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
This commit is contained in:
parent
b206103abc
commit
b4074c010b
@ -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,
|
||||
|
@ -237,23 +237,23 @@ const SmallVectorImpl<char> &MCCompressedFragment::getCompressedContents() const
|
||||
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;
|
||||
}
|
||||
|
@ -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 encodeZlibReturnValue(int ReturnValue) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user