LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

llvm::Compressor Class Reference

An abstraction for memory to memory data (de)compression. More...

#include <Compressor.h>

List of all members.

Low Level Interface

typedef unsigned( OutputDataCallback )(char *&buffer, unsigned &size, void *context)
 Output callback function type.
static uint64_t compress (const char *in, unsigned size, OutputDataCallback *cb, void *context=0)
 Compress a block of memory.
static uint64_t decompress (const char *in, unsigned size, OutputDataCallback *cb, void *context=0)
 Decompress a block of memory.

Static Public Member Functions

High Level Interface
static uint64_t compressToNewBuffer (const char *in, unsigned size, char *&out)
 Compress memory to a new memory buffer.
static uint64_t compressToStream (const char *in, unsigned size, std::ostream &out)
 Compress memory to a file.
static uint64_t decompressToNewBuffer (const char *in, unsigned size, char *&out)
 Decompress memory to a new memory buffer.
static uint64_t decompressToStream (const char *in, unsigned size, std::ostream &out)
 Decompress memory to a stream.


Detailed Description

An abstraction for memory to memory data (de)compression.

This class provides an abstraction for compression and decompression of a block of memory. The algorithm used here is currently bzip2 but that may change without notice. Should newer algorithms prove to compress bytecode better than bzip2, that newer algorithm will be added, but won't replace bzip2. This interface allows us to abstract the notion of compression and deal with alternate compression schemes over time. The type of compression used can be determined by inspecting the first byte of the compressed output. Currently value '0' means no compression was used (for very small files) and value '2' means bzip2 compression was used. The Compressor is intended for use with memory mapped files where the entire data block to be compressed or decompressed is available in memory. However, output can be gathered in repeated calls to a callback. Utilities for sending compressed or decompressed output to a stream or directly to a memory block are also provided.

Since:
1.4

Definition at line 38 of file Compressor.h.


Member Typedef Documentation

typedef unsigned( llvm::Compressor::OutputDataCallback)(char *&buffer, unsigned &size, void *context)
 

Output callback function type.

A callback function type used by the Compressor's low level interface to get the next chunk of data to which (de)compressed output will be written. This callback completely abstracts the notion of how to handle the output data of compression or decompression. The callback is responsible for determining both the storage location and the size of the output. The callback may also do other things with the data such as write it, transmit it, etc. Note that providing very small values for size will make the compression run very inefficiently. It is recommended that size be chosen based on the some multiple or fraction of the object being decompressed or compressed, respetively.

Returns:
0 for success, 1 for failure
Exceptions:
nothing 

Definition at line 108 of file Compressor.h.


Member Function Documentation

uint64_t llvm::Compressor::compress const char *  in,
unsigned  size,
OutputDataCallback cb,
void *  context = 0
[static]
 

Compress a block of memory.

This function does the compression work. The block of memory starting at in and extending for size bytes is compressed. The compressed output is written to memory blocks returned by the cb callback. The caller must provide an implementation of the OutputDataCallback function type and provide its address as cb. Note that the callback function will be called as many times as necessary to complete the compression of the in block but that the total size will generally be less than size. It is a good idea to provide as large a value to the callback's size parameter as possible so that fewer calls to the callback are made. The hint parameter tells the function which kind of compression to start with. However, if its not available on the platform, the algorithm "falls back" from bzip2 -> zlib -> simple.

Exceptions:
std::string if an error occurs
Returns:
the total size of the compressed data
Parameters:
in  The buffer to be compressed
size  The size of the buffer to be compressed
cb  Call back for memory allocation
context  Context for callback

Definition at line 241 of file Compressor.cpp.

References bz_stream::avail_in, bz_stream::avail_out, BZ2_bzCompress, BZ2_bzCompressEnd, BZ2_bzCompressInit, BZ_CONFIG_ERROR, BZ_FINISH, BZ_FINISH_OK, BZ_MEM_ERROR, BZ_OK, BZ_PARAM_ERROR, BZ_SEQUENCE_ERROR, BZ_STREAM_END, bz_stream::bzalloc, bz_stream::bzfree, getdata(), bz_stream::next_in, bz_stream::next_out, NULLCOMP_compress(), NULLCOMP_end(), NULLCOMP_init(), bz_stream::opaque, bz_stream::total_out_hi32, bz_stream::total_out_lo32, and llvm::utostr().

Referenced by compressToNewBuffer(), and compressToStream().

uint64_t llvm::Compressor::compressToNewBuffer const char *  in,
unsigned  size,
char *&  out
[static]
 

Compress memory to a new memory buffer.

This method compresses a block of memory pointed to by in with size size to a block of memory, out, that is allocated with malloc. It is the caller's responsibility to free out. The hint indicates which type of compression the caller would *prefer*.

Exceptions:
std::string explaining error if a compression error occurs
Returns:
The size of the output buffer out.
Parameters:
in  The buffer to be compressed
size  The size of the buffer to be compressed
out  The returned output buffer

Definition at line 329 of file Compressor.cpp.

References compress().

Referenced by llvm::Archive::writeMember().

uint64_t llvm::Compressor::compressToStream const char *  in,
unsigned  size,
std::ostream &  out
[static]
 

Compress memory to a file.

This method compresses a block of memory pointed to by in with size size to a stream. The stream out must be open and ready for writing when this method is called. The stream will not be closed by this method. The hint argument indicates which type of compression the caller would *prefer*.

Returns:
The amount of data written to out.
Parameters:
in  The buffer to be compressed
size  The size of the buffer to be compressed
out  The output stream to write data on

Definition at line 337 of file Compressor.cpp.

References compress().

Referenced by llvm::WriteBytecodeToFile().

uint64_t llvm::Compressor::decompress const char *  in,
unsigned  size,
OutputDataCallback cb,
void *  context = 0
[static]
 

Decompress a block of memory.

This function does the decompression work. The block of memory starting at in and extending for size bytes is decompressed. The decompressed output is written to memory blocks returned by the cb callback. The caller must provide an implementation of the OutputDataCallback function type and provide its address as cb. Note that the callback function will be called as many times as necessary to complete the compression of the in block but that the total size will generally be greater than size. It is a good idea to provide as large a value to the callback's size parameter as possible so that fewer calls to the callback are made.

Exceptions:
std::string if an error occurs
Returns:
the total size of the decompressed data
Parameters:
in  The buffer to be decompressed
size  Size of the buffer to be decompressed
cb  Call back for memory allocation
context  Context for callback

Definition at line 352 of file Compressor.cpp.

References bz_stream::avail_in, bz_stream::avail_out, BZ2_bzDecompress, BZ2_bzDecompressEnd, BZ2_bzDecompressInit, BZ_CONFIG_ERROR, BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC, BZ_MEM_ERROR, BZ_OK, BZ_PARAM_ERROR, BZ_STREAM_END, bz_stream::bzalloc, bz_stream::bzfree, getdata(), bz_stream::next_in, bz_stream::next_out, NULLCOMP_decompress(), NULLCOMP_end(), NULLCOMP_init(), bz_stream::opaque, bz_stream::total_out_hi32, and bz_stream::total_out_lo32.

Referenced by decompressToNewBuffer(), and decompressToStream().

uint64_t llvm::Compressor::decompressToNewBuffer const char *  in,
unsigned  size,
char *&  out
[static]
 

Decompress memory to a new memory buffer.

This method decompresses a block of memory pointed to by in with size size to a new block of memory, out, that was allocated by malloc. It is the caller's responsibility to free out.

Returns:
The size of the output buffer out.
Parameters:
in  The buffer to be decompressed
size  Size of the buffer to be decompressed
out  The returned output buffer

Definition at line 441 of file Compressor.cpp.

References decompress().

uint64_t llvm::Compressor::decompressToStream const char *  in,
unsigned  size,
std::ostream &  out
[static]
 

Decompress memory to a stream.

This method decompresses a block of memory pointed to by in with size size to a stream. The stream out must be open and ready for writing when this method is called. The stream will not be closed by this method.

Returns:
The amount of data written to out.
Parameters:
in  The buffer to be decompressed
size  Size of the buffer to be decompressed
out  The stream to write write data on

Definition at line 449 of file Compressor.cpp.

References decompress().


The documentation for this class was generated from the following files: