mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-22 10:24:26 +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:
@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
class MemoryBuffer;
|
|
||||||
class StringRef;
|
class StringRef;
|
||||||
|
|
||||||
namespace zlib {
|
namespace zlib {
|
||||||
@ -43,8 +42,7 @@ enum Status {
|
|||||||
|
|
||||||
bool isAvailable();
|
bool isAvailable();
|
||||||
|
|
||||||
Status compress(StringRef InputBuffer,
|
Status compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
|
||||||
std::unique_ptr<MemoryBuffer> &CompressedBuffer,
|
|
||||||
CompressionLevel Level = DefaultCompression);
|
CompressionLevel Level = DefaultCompression);
|
||||||
|
|
||||||
Status uncompress(StringRef InputBuffer,
|
Status uncompress(StringRef InputBuffer,
|
||||||
|
@ -237,23 +237,23 @@ const SmallVectorImpl<char> &MCCompressedFragment::getCompressedContents() const
|
|||||||
assert(getParent()->size() == 1 &&
|
assert(getParent()->size() == 1 &&
|
||||||
"Only compress sections containing a single fragment");
|
"Only compress sections containing a single fragment");
|
||||||
if (CompressedContents.empty()) {
|
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::Status Success =
|
||||||
zlib::compress(StringRef(getContents().data(), getContents().size()),
|
zlib::compress(StringRef(getContents().data(), getContents().size()),
|
||||||
CompressedSection);
|
CompressedContents);
|
||||||
(void)Success;
|
(void)Success;
|
||||||
assert(Success == zlib::StatusOK);
|
assert(Success == zlib::StatusOK);
|
||||||
CompressedContents.push_back('Z');
|
static const StringRef Magic = "ZLIB";
|
||||||
CompressedContents.push_back('L');
|
|
||||||
CompressedContents.push_back('I');
|
|
||||||
CompressedContents.push_back('B');
|
|
||||||
uint64_t Size = getContents().size();
|
uint64_t Size = getContents().size();
|
||||||
if (sys::IsLittleEndianHost)
|
if (sys::IsLittleEndianHost)
|
||||||
Size = sys::SwapByteOrder(Size);
|
Size = sys::SwapByteOrder(Size);
|
||||||
CompressedContents.append(reinterpret_cast<char *>(&Size),
|
CompressedContents.insert(CompressedContents.begin(),
|
||||||
reinterpret_cast<char *>(&Size + 1));
|
Magic.size() + sizeof(Size));
|
||||||
CompressedContents.append(CompressedSection->getBuffer().begin(),
|
std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
|
||||||
CompressedSection->getBuffer().end());
|
std::copy(reinterpret_cast<char *>(&Size),
|
||||||
|
reinterpret_cast<char *>(&Size + 1),
|
||||||
|
CompressedContents.begin() + Magic.size());
|
||||||
}
|
}
|
||||||
return CompressedContents;
|
return CompressedContents;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include "llvm/Support/Compiler.h"
|
#include "llvm/Support/Compiler.h"
|
||||||
#include "llvm/Support/ErrorHandling.h"
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
#include "llvm/Support/MemoryBuffer.h"
|
|
||||||
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#endif
|
#endif
|
||||||
@ -47,20 +46,15 @@ static zlib::Status encodeZlibReturnValue(int ReturnValue) {
|
|||||||
|
|
||||||
bool zlib::isAvailable() { return true; }
|
bool zlib::isAvailable() { return true; }
|
||||||
zlib::Status zlib::compress(StringRef InputBuffer,
|
zlib::Status zlib::compress(StringRef InputBuffer,
|
||||||
std::unique_ptr<MemoryBuffer> &CompressedBuffer,
|
SmallVectorImpl<char> &CompressedBuffer,
|
||||||
CompressionLevel Level) {
|
CompressionLevel Level) {
|
||||||
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
||||||
std::unique_ptr<char[]> TmpBuffer(new char[CompressedSize]);
|
CompressedBuffer.resize(CompressedSize);
|
||||||
int CLevel = encodeZlibCompressionLevel(Level);
|
int CLevel = encodeZlibCompressionLevel(Level);
|
||||||
Status Res = encodeZlibReturnValue(::compress2(
|
Status Res = encodeZlibReturnValue(::compress2(
|
||||||
(Bytef *)TmpBuffer.get(), &CompressedSize,
|
(Bytef *)CompressedBuffer.data(), &CompressedSize,
|
||||||
(const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
|
(const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
|
||||||
if (Res == StatusOK) {
|
CompressedBuffer.resize(CompressedSize);
|
||||||
CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
|
|
||||||
StringRef(TmpBuffer.get(), CompressedSize)));
|
|
||||||
// Tell MSan that memory initialized by zlib is valid.
|
|
||||||
__msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize);
|
|
||||||
}
|
|
||||||
return Res;
|
return Res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,12 +76,12 @@ uint32_t zlib::crc32(StringRef Buffer) {
|
|||||||
#else
|
#else
|
||||||
bool zlib::isAvailable() { return false; }
|
bool zlib::isAvailable() { return false; }
|
||||||
zlib::Status zlib::compress(StringRef InputBuffer,
|
zlib::Status zlib::compress(StringRef InputBuffer,
|
||||||
std::unique_ptr<MemoryBuffer> &CompressedBuffer,
|
SmallVectorImpl<char> &CompressedBuffer,
|
||||||
CompressionLevel Level) {
|
CompressionLevel Level) {
|
||||||
return zlib::StatusUnsupported;
|
return zlib::StatusUnsupported;
|
||||||
}
|
}
|
||||||
zlib::Status zlib::uncompress(StringRef InputBuffer,
|
zlib::Status zlib::uncompress(StringRef InputBuffer,
|
||||||
std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
|
SmallVectorImpl<char> &UncompressedBuffer,
|
||||||
size_t UncompressedSize) {
|
size_t UncompressedSize) {
|
||||||
return zlib::StatusUnsupported;
|
return zlib::StatusUnsupported;
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
#include "llvm/Support/Compression.h"
|
#include "llvm/Support/Compression.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
#include "llvm/ADT/SmallString.h"
|
||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include "llvm/Support/MemoryBuffer.h"
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -24,19 +24,17 @@ namespace {
|
|||||||
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
||||||
|
|
||||||
void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
|
void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
|
||||||
std::unique_ptr<MemoryBuffer> Compressed;
|
SmallString<32> Compressed;
|
||||||
SmallString<32> Uncompressed;
|
SmallString<32> Uncompressed;
|
||||||
EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
|
EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
|
||||||
// Check that uncompressed buffer is the same as original.
|
// Check that uncompressed buffer is the same as original.
|
||||||
EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
|
EXPECT_EQ(zlib::StatusOK,
|
||||||
Uncompressed, Input.size()));
|
zlib::uncompress(Compressed, Uncompressed, Input.size()));
|
||||||
EXPECT_EQ(Input.size(), Uncompressed.size());
|
EXPECT_EQ(Input, Uncompressed);
|
||||||
EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
|
|
||||||
if (Input.size() > 0) {
|
if (Input.size() > 0) {
|
||||||
// Uncompression fails if expected length is too short.
|
// Uncompression fails if expected length is too short.
|
||||||
EXPECT_EQ(zlib::StatusBufferTooShort,
|
EXPECT_EQ(zlib::StatusBufferTooShort,
|
||||||
zlib::uncompress(Compressed->getBuffer(), Uncompressed,
|
zlib::uncompress(Compressed, Uncompressed, Input.size() - 1));
|
||||||
Input.size() - 1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user