Files
externals/linux/include/ActivMiddleware/ActivBase/Compress/ICompressor.h
T
2023-12-15 13:15:08 -05:00

262 lines
8.3 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Compress/ICompressor.h
*
* @brief Interface for various compression algorithms.
*/
#if (!defined ACTIV_BASE_COMPRESS_ICOMPRESSOR_H)
#define ACTIV_BASE_COMPRESS_ICOMPRESSOR_H
#include "ActivMiddleware/ActivBase/External.h"
//! Activ namespace.
namespace Activ
{
/**
* @brief Calculate size of destination buffer needed to compress a buffer of length sourceLength.
*
* To support Zlib, destination buffer must be at least 0.1% + 12 bytes the size of the source buffer.
* To support lz4, destination buffer must be at least 0.4% + 16 bytes the size of the source buffer.
*
* @param sourceLength length of the source buffer.
*
* @return size of destination buffer needed.
*/
#define ACTIV_COMPRESS_GET_DESTINATION_BUFFER_LENGTH(sourceLength) \
(static_cast<size_t>(((static_cast<uint64_t>((sourceLength)) * UINT64_C(1004)) / UINT64_C(1000)) + 16))
// ---------------------------------------------------------------------------------------------------------------------------------
//! Abstract base block compression and decompression class.
class ICompressor : private Boost::noncopyable
{
public:
//! Compression type enumeration.
enum Type
{
TYPE_NULL = 0,
TYPE_RDC = 1,
TYPE_ZLIB = 2,
TYPE_LZ4 = 3,
TYPE_ZSTD = 4,
NUM_TYPES
};
//! Compression and decompression statistics class.
class Stats
{
public:
//! Default constructor.
Stats();
/**
* @brief Update compression or decompression statistics.
*
* @param inputBytes number of input bytes.
* @param outputBytes number of output bytes.
*/
void UpdateStats(const uint64_t inputBytes, const uint64_t outputBytes);
//! Clear compression or decompression statistics.
void ClearStats();
uint64_t m_apiCount; ///< Number of API calls made.
uint64_t m_inputBytes; ///< Total number of input bytes.
uint64_t m_outputBytes; ///< Total number of output bytes.
};
//! Destructor.
ACTIV_BASE_API
virtual ~ICompressor() = 0;
//! Get the compressor type.
Type GetType() const;
/**
* @brief Compress a buffer.
*
* @param pDestination the resultant compressed buffer.
* @param destinationLength the length of the destination buffer.
* @param pSource the buffer to be compressed.
* @param sourceLength length of the source buffer.
* @param compressedLength the number of bytes used in the destination buffer.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW
* @retval STATUS_CODE_FAILURE
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_HEAP_EMPTY
*/
StatusCode Compress(void *pDestination, const size_t destinationLength, const void *pSource, const size_t sourceLength,
size_t &compressedLength);
/**
* @brief Decompress a buffer.
*
* @param pDestination the resultant decompressed buffer.
* @param destinationLength the length of the destination buffer.
* @param pSource the buffer to be decompressed.
* @param sourceLength length of the source buffer.
* @param decompressedLength the number of bytes used in the destination buffer.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW
* @retval STATUS_CODE_FAILURE
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_HEAP_EMPTY
*/
StatusCode Decompress(void *pDestination, const size_t destinationLength, const void *pSource,
const size_t sourceLength, size_t &decompressedLength);
/**
* @brief Compress a buffer.
*
* @param pDestination the resultant compressed buffer.
* @param destinationLength the length of the destination buffer.
* @param pSource the buffer to be compressed.
* @param sourceLength length of the source buffer.
* @param compressedLength the number of bytes used in the destination buffer.
* @param stats compression stats to update.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW
* @retval STATUS_CODE_FAILURE
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_HEAP_EMPTY
*/
virtual StatusCode Compress(void *pDestination, const size_t destinationLength, const void *pSource, const size_t sourceLength,
size_t &compressedLength, Stats &stats) = 0;
/**
* @brief Decompress a buffer.
*
* @param pDestination the resultant decompressed buffer.
* @param destinationLength the length of the destination buffer.
* @param pSource the buffer to be decompressed.
* @param sourceLength length of the source buffer.
* @param decompressedLength the number of bytes used in the destination buffer.
* @param stats decompression stats to update.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW
* @retval STATUS_CODE_FAILURE
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_HEAP_EMPTY
*/
virtual StatusCode Decompress(void *pDestination, const size_t destinationLength, const void *pSource,
const size_t sourceLength, size_t &decompressedLength, Stats &stats) = 0;
//! Get compress statistics.
const Stats &GetCompressStats() const;
//! Get decompress statistics.
const Stats &GetDecompressStats() const;
//! Clear compress statistics.
void ClearCompressStats();
//! Clear decompress statistics.
void ClearDecompressStats();
//! Get the type as a string.
ACTIV_BASE_API
static const char *TypeToString(const Type type);
protected:
/**
* @brief Constructor.
*
* @param type
*/
ACTIV_BASE_API
ICompressor(const Type type);
const Type m_type;
Stats m_compressStats; ///< Compression statistics.
Stats m_decompressStats; ///< Decompression statistics.
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline ICompressor::Type ICompressor::GetType() const
{
return m_type;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode ICompressor::Compress(void *pDestination, const size_t destinationLength, const void *pSource,
const size_t sourceLength, size_t &compressedLength)
{
return Compress(pDestination, destinationLength, pSource, sourceLength, compressedLength, m_compressStats);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode ICompressor::Decompress(void *pDestination, const size_t destinationLength, const void *pSource,
const size_t sourceLength, size_t &decompressedLength)
{
return Decompress(pDestination, destinationLength, pSource, sourceLength, decompressedLength, m_decompressStats);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const ICompressor::Stats &ICompressor::GetCompressStats() const
{
return m_compressStats;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const ICompressor::Stats &ICompressor::GetDecompressStats() const
{
return m_decompressStats;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ICompressor::ClearCompressStats()
{
m_compressStats.ClearStats();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ICompressor::ClearDecompressStats()
{
m_decompressStats.ClearStats();
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline ICompressor::Stats::Stats() :
m_apiCount(0),
m_inputBytes(0),
m_outputBytes(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ICompressor::Stats::UpdateStats(const uint64_t inputBytes, const uint64_t outputBytes)
{
++m_apiCount;
m_inputBytes += inputBytes;
m_outputBytes += outputBytes;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ICompressor::Stats::ClearStats()
{
m_apiCount = 0;
m_inputBytes = 0;
m_outputBytes = 0;
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_COMPRESS_ICOMPRESSOR_H)