1 //===- llvm/Support/Compressor.h --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::Compressor class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_COMPRESSOR_H
15 #define LLVM_SUPPORT_COMPRESSOR_H
17 #include <llvm/Support/DataTypes.h>
21 /// This class provides an abstraction for compressing a block of memory using
22 /// a standard compression utility such as bzip2 or libz. This interface
23 /// allos us to abstraction the notion of compression and deal with alternate
24 /// compression scheme availability depending on the configured platform. This
25 /// facility will always favor a bzip2 implementation if its available.
26 /// Otherwise, libz will be used if its available. If neither zlib nor bzip2
27 /// are available, a very simple algorithm provided by the Compressor class
28 /// will be used The type of compression used can be determined by inspecting
29 /// the first byte of the compressed output. ASCII values '0', '1', and '2',
30 /// denote the compression type as given in the Algorithm enumeration below.
31 /// The Compressor is intended for use with memory mapped files where the
32 /// entire data block to be compressed or decompressed is available in
33 /// memory. Output, however, can be gathered in repeated calls to a callback.
35 /// @brief An abstraction for memory to memory data compression
41 COMP_TYPE_SIMPLE = '0', ///< Use simple but ubiquitous algorithm
42 COMP_TYPE_ZLIB = '1', ///< Use zlib algorithm, if available
43 COMP_TYPE_BZIP2 = '2', ///< Use bzip2 algorithm (preferred)
46 /// A callback function type used by the Compressor to get the next chunk
47 /// of data to which (de)compressed output will be written. This function
48 /// must be written by the caller to provide the buffering of the output
50 /// @returns 0 for success, 1 for failure
52 /// @brief Output callback function type
53 typedef unsigned (OutputDataCallback)(char*& buffer, unsigned& size);
59 /// This function does the compression work. The block of memory starting
60 /// at \p in and extending for \p size bytes is compressed. The compressed
61 /// output is written to memory blocks returned by the \p cb callback. The
62 /// caller must provide an implementation of the OutputDataCallback
63 /// function type and provide its address as \p cb. Note that the callback
64 /// function will be called as many times as necessary to complete the
65 /// compression of the \p in block but that the total size will generally
66 /// be less than \p size. It is a good idea to provide as large a value to
67 /// the callback's \p size parameter as possible so that fewer calls to
68 /// the callback are made. The \p hint parameter tells the function which
69 /// kind of compression to start with. However, if its not available on
70 /// the platform, the algorithm "falls back" from bzip2 -> zlib -> simple.
71 /// @throws std::string if an error occurs
72 /// @returns the total size of the compressed data
73 /// @brief Compress a block of memory.
74 static uint64_t compress(char* in, unsigned size, OutputDataCallback* cb,
75 Algorithm hint = COMP_TYPE_BZIP2);
77 /// This function does the decompression work. The block of memory
78 /// starting at \p in and extending for \p size bytes is decompressed. The
79 /// decompressed output is written to memory blocks returned by the \p cb
80 /// callback. The caller must provide an implementation of the
81 /// OutputDataCallback function type and provide its address as \p cb.
82 /// Note that the callback function will be called as many times as
83 /// necessary to complete the compression of the \p in block but that the
84 /// total size will generally be greater than \p size. It is a good idea
85 /// to provide as large a value to the callback's \p size parameter as
86 /// possible so that fewer calls to the callback are made.
87 /// @throws std::string if an error occurs
88 /// @returns the total size of the decompressed data
89 /// @brief Decompress a block of memory.
90 static uint64_t decompress(char *in, unsigned size,
91 OutputDataCallback* cb);