LLVM API Documentation
#include <Compressor.h>
Low Level Interface | |
typedef size_t( | OutputDataCallback )(char *&buffer, size_t &size, void *context) |
Output callback function type. | |
static size_t | compress (const char *in, size_t size, OutputDataCallback *cb, void *context=0) |
Compress a block of memory. | |
static size_t | decompress (const char *in, size_t size, OutputDataCallback *cb, void *context=0) |
Decompress a block of memory. | |
Static Public Member Functions | |
High Level Interface | |
static size_t | compressToNewBuffer (const char *in, size_t size, char *&out) |
Compress memory to a new memory buffer. | |
static size_t | compressToStream (const char *in, size_t size, std::ostream &out) |
Compress memory to a file. | |
static size_t | decompressToNewBuffer (const char *in, size_t size, char *&out) |
Decompress memory to a new memory buffer. | |
static size_t | decompressToStream (const char *in, size_t size, std::ostream &out) |
Decompress memory to a stream. |
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.
Definition at line 38 of file Compressor.h.
typedef size_t( llvm::Compressor::OutputDataCallback)(char *&buffer, size_t &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.
nothing |
Definition at line 108 of file Compressor.h.
size_t Compressor::compress | ( | const char * | in, | |
size_t | 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.
std::string | if an error occurs |
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 263 of file Compressor.cpp.
References NULLCOMP_stream::avail_in, bz_stream::avail_in, NULLCOMP_stream::avail_out, 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, COMP_TYPE_BZIP2, COMP_TYPE_NONE, getdata(), getdata_uns(), NULLCOMP_stream::next_in, bz_stream::next_in, NULLCOMP_stream::next_out, bz_stream::next_out, NULLCOMP_compress(), NULLCOMP_end(), NULLCOMP_init(), bz_stream::opaque, NULLCOMP_stream::output_count, bz_stream::total_out_hi32, bz_stream::total_out_lo32, and llvm::utostr().
Referenced by compressToNewBuffer(), and compressToStream().
size_t Compressor::compressToNewBuffer | ( | const char * | in, | |
size_t | 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*.
std::string | explaining error if a compression error occurs |
out
. in | The buffer to be compressed |
size | The size of the buffer to be compressed |
out | The returned output buffer |
Definition at line 351 of file Compressor.cpp.
References compress().
Referenced by llvm::Archive::writeMember().
size_t Compressor::compressToStream | ( | const char * | in, | |
size_t | 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*.
out
. 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 360 of file Compressor.cpp.
References compress().
Referenced by llvm::WriteBytecodeToFile().
size_t Compressor::decompress | ( | const char * | in, | |
size_t | 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.
std::string | if an error occurs |
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 375 of file Compressor.cpp.
References NULLCOMP_stream::avail_in, bz_stream::avail_in, NULLCOMP_stream::avail_out, 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, COMP_TYPE_BZIP2, COMP_TYPE_NONE, getdata(), getdata_uns(), NULLCOMP_stream::next_in, bz_stream::next_in, NULLCOMP_stream::next_out, bz_stream::next_out, NULLCOMP_decompress(), NULLCOMP_end(), NULLCOMP_init(), bz_stream::opaque, NULLCOMP_stream::output_count, bz_stream::total_out_hi32, and bz_stream::total_out_lo32.
Referenced by decompressToNewBuffer(), and decompressToStream().
size_t Compressor::decompressToNewBuffer | ( | const char * | in, | |
size_t | 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
.
out
. in | The buffer to be decompressed |
size | Size of the buffer to be decompressed |
out | The returned output buffer |
Definition at line 467 of file Compressor.cpp.
References decompress().
size_t Compressor::decompressToStream | ( | const char * | in, | |
size_t | 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.
out
. 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 475 of file Compressor.cpp.
References decompress().