#ifndef LLVM_SUPPORT_COMPRESSOR_H
#define LLVM_SUPPORT_COMPRESSOR_H
-#include <llvm/Support/DataTypes.h>
+#include "llvm/Support/DataTypes.h"
namespace llvm {
/// This class provides an abstraction for compressing a block of memory using
/// a standard compression utility such as bzip2 or libz. This interface
- /// allos us to abstraction the notion of compression and deal with alternate
+ /// allows us to abstract the notion of compression and deal with alternate
/// compression scheme availability depending on the configured platform. This
/// facility will always favor a bzip2 implementation if its available.
- /// Otherwise, libz will be used if its available. If neither zlib nor bzip2
+ /// Otherwise, libz will be used if it is available. If neither zlib nor bzip2
/// are available, a very simple algorithm provided by the Compressor class
- /// will be used The type of compression used can be determined by inspecting
+ /// will be used. The type of compression used can be determined by inspecting
/// the first byte of the compressed output. ASCII values '0', '1', and '2',
/// denote the compression type as given in the Algorithm enumeration below.
/// The Compressor is intended for use with memory mapped files where the
/// entire data block to be compressed or decompressed is available in
- /// memory. Output, however, can be gathered in repeated calls to a callback.
+ /// memory. However, output can be gathered in repeated calls to a callback.
/// @since 1.4
- /// @brief An abstraction for memory to memory data compression
+ /// @brief An abstraction for memory to memory data (de)compression
class Compressor {
/// @name Types
/// @{
/// @returns 0 for success, 1 for failure
/// @throws nothing
/// @brief Output callback function type
- typedef unsigned (OutputDataCallback)(char*& buffer, unsigned& size);
+ typedef unsigned (OutputDataCallback)(char*& buffer, unsigned& size,
+ void* context);
/// @}
/// @name Methods
/// @returns the total size of the compressed data
/// @brief Compress a block of memory.
static uint64_t compress(char* in, unsigned size, OutputDataCallback* cb,
- Algorithm hint = COMP_TYPE_BZIP2);
+ Algorithm hint = COMP_TYPE_BZIP2,
+ void* context = 0);
/// This function does the decompression work. The block of memory
/// starting at \p in and extending for \p size bytes is decompressed. The
/// @returns the total size of the decompressed data
/// @brief Decompress a block of memory.
static uint64_t decompress(char *in, unsigned size,
- OutputDataCallback* cb);
+ OutputDataCallback* cb, void* context = 0);
/// @}
};
namespace {
inline int getdata(char*& buffer, unsigned& size,
- llvm::Compressor::OutputDataCallback* cb) {
+ llvm::Compressor::OutputDataCallback* cb, void* context) {
buffer = 0;
size = 0;
- int result = (*cb)(buffer, size);
+ int result = (*cb)(buffer, size, context);
assert(buffer != 0 && "Invalid result from Compressor callback");
assert(size != 0 && "Invalid result from Compressor callback");
return result;
namespace llvm {
// Compress in one of three ways
-uint64_t Compressor::compress(char* in, unsigned size,
- OutputDataCallback* cb, Algorithm hint) {
+uint64_t Compressor::compress(char* in, unsigned size, OutputDataCallback* cb,
+ Algorithm hint, void* context ) {
assert(in && "Can't compress null buffer");
assert(size && "Can't compress empty buffer");
assert(cb && "Can't compress without a callback function");
}
// Get a block of memory
- if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb)) {
+ if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
}
// Compress it
int bzerr = BZ_FINISH_OK;
while (BZ_FINISH_OK == (bzerr = BZ2_bzCompress(&bzdata, BZ_FINISH))) {
- if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb)) {
+ if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzCompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
}
if (Z_OK != deflateInit(&zdata,Z_BEST_COMPRESSION))
throw std::string(zdata.msg ? zdata.msg : "zlib error");
- if (0 != getdata((char*&)(zdata.next_out), zdata.avail_out,cb)) {
+ if (0 != getdata((char*&)(zdata.next_out), zdata.avail_out,cb,context)) {
deflateEnd(&zdata);
throw std::string("Can't allocate output buffer");
}
int flush = 0;
while ( Z_OK == deflate(&zdata,0) && zdata.avail_out == 0) {
- if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb)) {
+ if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb,context)) {
deflateEnd(&zdata);
throw std::string("Can't allocate output buffer");
}
}
while ( Z_STREAM_END != deflate(&zdata, Z_FINISH)) {
- if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb)) {
+ if (0 != getdata((char*&)zdata.next_out, zdata.avail_out, cb,context)) {
deflateEnd(&zdata);
throw std::string("Can't allocate output buffer");
}
sdata.avail_in = size;
RLCOMP_init(&sdata);
- if (0 != getdata(sdata.next_out, sdata.avail_out,cb)) {
+ if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
}
sdata.avail_out--;
while (!RLCOMP_compress(&sdata)) {
- if (0 != getdata(sdata.next_out, sdata.avail_out,cb)) {
+ if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
}
}
// Decompress in one of three ways
uint64_t Compressor::decompress(char *in, unsigned size,
- OutputDataCallback* cb) {
+ OutputDataCallback* cb, void* context) {
assert(in && "Can't decompress null buffer");
assert(size > 1 && "Can't decompress empty buffer");
assert(cb && "Can't decompress without a callback function");
}
// Get a block of memory
- if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb)) {
+ if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
}
// Decompress it
int bzerr = BZ_OK;
while (BZ_OK == (bzerr = BZ2_bzDecompress(&bzdata))) {
- if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb)) {
+ if (0 != getdata(bzdata.next_out, bzdata.avail_out,cb,context)) {
BZ2_bzDecompressEnd(&bzdata);
throw std::string("Can't allocate output buffer");
}
if ( Z_OK != inflateInit(&zdata))
throw std::string(zdata.msg ? zdata.msg : "zlib error");
- if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb)) {
+ if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb,context)) {
inflateEnd(&zdata);
throw std::string("Can't allocate output buffer");
}
int zerr = Z_OK;
while (Z_OK == (zerr = inflate(&zdata,0))) {
- if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb)) {
+ if (0 != getdata((char*&)zdata.next_out, zdata.avail_out,cb,context)) {
inflateEnd(&zdata);
throw std::string("Can't allocate output buffer");
}
sdata.avail_in = size - 1;
RLCOMP_init(&sdata);
- if (0 != getdata(sdata.next_out, sdata.avail_out,cb)) {
+ if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
}
while (!RLCOMP_decompress(&sdata)) {
- if (0 != getdata(sdata.next_out, sdata.avail_out,cb)) {
+ if (0 != getdata(sdata.next_out, sdata.avail_out,cb,context)) {
throw std::string("Can't allocate output buffer");
}
}