Add a context for the callback so different compression scenarios can be

distinguished. Tidy up documentation.  Thanks, Chris.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16652 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2004-10-04 17:29:25 +00:00
parent ce4e6ade3e
commit 469c34bdeb
2 changed files with 29 additions and 27 deletions

View File

@ -14,25 +14,25 @@
#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
/// @{
@ -50,7 +50,8 @@ namespace llvm {
/// @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
@ -72,7 +73,8 @@ namespace llvm {
/// @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
@ -88,7 +90,7 @@ namespace llvm {
/// @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);
/// @}
};

View File

@ -35,10 +35,10 @@
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;
@ -255,8 +255,8 @@ void RLCOMP_end(RLCOMP_stream* strm) {
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");
@ -285,7 +285,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
}
// 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");
}
@ -297,7 +297,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
// 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");
}
@ -332,7 +332,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
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");
}
@ -342,14 +342,14 @@ uint64_t Compressor::compress(char* in, unsigned size,
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");
}
@ -370,7 +370,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
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");
}
@ -378,7 +378,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
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");
}
}
@ -395,7 +395,7 @@ uint64_t Compressor::compress(char* in, unsigned size,
// 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");
@ -426,7 +426,7 @@ uint64_t Compressor::decompress(char *in, unsigned size,
}
// 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");
}
@ -434,7 +434,7 @@ uint64_t Compressor::decompress(char *in, unsigned size,
// 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");
}
@ -471,14 +471,14 @@ uint64_t Compressor::decompress(char *in, unsigned size,
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");
}
@ -499,12 +499,12 @@ uint64_t Compressor::decompress(char *in, unsigned size,
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");
}
}