From ae7aa0665004b17186e4e19f19753944e010db07 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 3 Feb 2016 02:46:46 +0100 Subject: [PATCH] lots of minor refactorings --- lib/bitstream.h | 63 +++++++++++++++++++------------------------ lib/fse.h | 20 +++++++------- lib/mem.h | 48 +++++++++++++-------------------- lib/zstd_buffered.c | 18 ++++++++----- lib/zstd_compress.c | 4 +-- lib/zstd_decompress.c | 6 ++--- lib/zstd_static.h | 1 + 7 files changed, 71 insertions(+), 89 deletions(-) diff --git a/lib/bitstream.h b/lib/bitstream.h index fbd0f3f3..e0487e87 100644 --- a/lib/bitstream.h +++ b/lib/bitstream.h @@ -1,8 +1,8 @@ /* ****************************************************************** bitstream - Part of NewGen Entropy library + Part of FSE library header file (to include) - Copyright (C) 2013-2015, Yann Collet. + Copyright (C) 2013-2016, Yann Collet. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) @@ -31,7 +31,6 @@ You can contact the author at : - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - - Public forum : https://groups.google.com/forum/#!forum/lz4c ****************************************************************** */ #ifndef BITSTREAM_H_MODULE #define BITSTREAM_H_MODULE @@ -47,17 +46,17 @@ extern "C" { * these functions are defined into a .h to be included. */ -/****************************************** -* Includes +/*-**************************************** +* Dependencies ******************************************/ #include "mem.h" /* unaligned access routines */ #include "error_private.h" /* error codes and messages */ -/******************************************** -* bitStream compression API (write forward) +/*-****************************************** +* bitStream encoding API (write forward) ********************************************/ -/* +/*! * bitStream can mix input from multiple sources. * A critical property of these streams is that they encode and decode in **reverse** direction. * So the first bit sequence you add will be the last to be read, like a LIFO stack. @@ -71,32 +70,32 @@ typedef struct char* endPtr; } BIT_CStream_t; -MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t maxDstSize); +MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits); MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); -/* -* Start by initCStream, providing the maximum size of write buffer to write into. +/*! +* Start by initCStream, providing the size of buffer to write into. * bitStream will never write outside of this buffer. -* buffer must be at least as large as a size_t, otherwise function result will be an error code. +* @dstCapacity must be >= sizeof(size_t), otherwise @return will be an error code. * * bits are first added to a local register. * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. -* Writing data into memory is a manual operation, performed by the flushBits function. +* Writing data into memory is an explicit operation, performed by the flushBits function. * Hence keep track how many bits are potentially stored into local register to avoid register overflow. * After a flushBits, a maximum of 7 bits might still be stored into local register. * -* Avoid storing elements of more than 25 bits if you want compatibility with 32-bits bitstream readers. +* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. * * Last operation is to close the bitStream. * The function returns the final size of CStream in bytes. -* If data couldn't fit into dstBuffer, it will return a 0 ( == not storable) +* If data couldn't fit into @dstBuffer, it will return a 0 ( == not storable) */ -/********************************************** -* bitStream decompression API (read backward) +/*-******************************************** +* bitStream decoding API (read backward) **********************************************/ typedef struct { @@ -118,19 +117,19 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); -/* +/*! * Start by invoking BIT_initDStream(). * A chunk of the bitStream is then stored into a local register. * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). * You can then retrieve bitFields stored into the local register, **in reverse order**. -* Local register is manually filled from memory by the BIT_reloadDStream() method. +* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished. * Otherwise, it can be less than that, so proceed accordingly. * Checking if DStream has reached its end can be performed with BIT_endOfDStream() */ -/****************************************** +/*-**************************************** * unsafe API ******************************************/ MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits); @@ -144,7 +143,7 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); -/**************************************************************** +/*-************************************************************** * Helper functions ****************************************************************/ MEM_STATIC unsigned BIT_highbit32 (register U32 val) @@ -170,10 +169,9 @@ MEM_STATIC unsigned BIT_highbit32 (register U32 val) } -/**************************************************************** +/*-************************************************************** * bitStream encoding ****************************************************************/ - MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t maxSize) { bitC->bitContainer = 0; @@ -240,10 +238,9 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) } -/********************************************************** +/*-******************************************************** * bitStream decoding **********************************************************/ - /*!BIT_initDStream * Initialize a BIT_DStream_t. * @bitD : a pointer to an already allocated BIT_DStream_t structure @@ -255,8 +252,7 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si { if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } - if (srcSize >= sizeof(size_t)) /* normal case */ - { + if (srcSize >= sizeof(size_t)) { /* normal case */ U32 contain32; bitD->start = (const char*)srcBuffer; bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); @@ -264,9 +260,7 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ bitD->bitsConsumed = 8 - BIT_highbit32(contain32); - } - else - { + } else { U32 contain32; bitD->start = (const char*)srcBuffer; bitD->ptr = bitD->start; @@ -342,23 +336,20 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ return BIT_DStream_overflow; - if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) - { + if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { bitD->ptr -= bitD->bitsConsumed >> 3; bitD->bitsConsumed &= 7; bitD->bitContainer = MEM_readLEST(bitD->ptr); return BIT_DStream_unfinished; } - if (bitD->ptr == bitD->start) - { + if (bitD->ptr == bitD->start) { if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; return BIT_DStream_completed; } { U32 nbBytes = bitD->bitsConsumed >> 3; BIT_DStream_status result = BIT_DStream_unfinished; - if (bitD->ptr - nbBytes < bitD->start) - { + if (bitD->ptr - nbBytes < bitD->start) { nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ result = BIT_DStream_endOfBuffer; } diff --git a/lib/fse.h b/lib/fse.h index 7e6bfc1a..db6f49cf 100644 --- a/lib/fse.h +++ b/lib/fse.h @@ -170,18 +170,18 @@ void FSE_freeCTable (FSE_CTable* ct); /*! FSE_buildCTable(): - Builds 'ct', which must be already allocated, using FSE_createCTable() + Builds @ct, which must be already allocated, using FSE_createCTable() return : 0 or an errorCode, which can be tested using FSE_isError() */ size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); /*! FSE_compress_usingCTable(): - Compress 'src' using 'ct' into 'dst' which must be already allocated - return : size of compressed data (<= maxDstSize) - or 0 if compressed data could not fit into 'dst' + Compress @src using @ct into @dst which must be already allocated + return : size of compressed data (<= @dstCapacity) + or 0 if compressed data could not fit into @dst or an errorCode, which can be tested using FSE_isError() */ -size_t FSE_compress_usingCTable (void* dst, size_t maxDstSize, const void* src, size_t srcSize, const FSE_CTable* ct); +size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); /*! Tutorial : @@ -221,7 +221,7 @@ If there is an error, both functions will return an ErrorCode (which can be test 'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize' -The function returns the size of compressed data (without header), necessarily <= maxDstSize. +The function returns the size of compressed data (without header), necessarily <= @dstCapacity. If it returns '0', compressed data could not fit into 'dst'. If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). */ @@ -253,11 +253,11 @@ size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned /*! FSE_decompress_usingDTable(): - Decompress compressed source 'cSrc' of size 'cSrcSize' using 'dt' - into 'dst' which must be already allocated. - return : size of regenerated data (necessarily <= maxDstSize) + Decompress compressed source @cSrc of size @cSrcSize using @dt + into @dst which must be already allocated. + return : size of regenerated data (necessarily <= @dstCapacity) or an errorCode, which can be tested using FSE_isError() */ -size_t FSE_decompress_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); +size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); /*! Tutorial : diff --git a/lib/mem.h b/lib/mem.h index 8ac56ed9..36ba06f0 100644 --- a/lib/mem.h +++ b/lib/mem.h @@ -39,15 +39,15 @@ extern "C" { #endif -/****************************************** -* Includes +/*-**************************************** +* Dependencies ******************************************/ #include /* size_t, ptrdiff_t */ #include /* memcpy */ -/****************************************** -* Compiler-specific +/*-**************************************** +* Compiler specifics ******************************************/ #if defined(__GNUC__) # define MEM_STATIC static __attribute__((unused)) @@ -60,7 +60,7 @@ extern "C" { #endif -/**************************************************************** +/*-************************************************************** * Basic Types *****************************************************************/ #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) @@ -83,10 +83,10 @@ extern "C" { #endif -/**************************************************************** +/*-************************************************************** * Memory I/O *****************************************************************/ -/* MEM_FORCE_MEMORY_ACCESS +/*!MEM_FORCE_MEMORY_ACCESS * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. * The below switch allow to select different access method for improved performance. @@ -94,8 +94,8 @@ extern "C" { * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. * Method 2 : direct access. This method is portable but violate C standard. - * It can generate buggy code on targets generating assembly depending on alignment. - * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) + * It can generate buggy code on targets depending on alignment. + * In some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. * Prefer these methods in priority order (0 > 1 > 2) */ @@ -185,8 +185,7 @@ MEM_STATIC U16 MEM_readLE16(const void* memPtr) { if (MEM_isLittleEndian()) return MEM_read16(memPtr); - else - { + else { const BYTE* p = (const BYTE*)memPtr; return (U16)(p[0] + (p[1]<<8)); } @@ -194,12 +193,9 @@ MEM_STATIC U16 MEM_readLE16(const void* memPtr) MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) { - if (MEM_isLittleEndian()) - { + if (MEM_isLittleEndian()) { MEM_write16(memPtr, val); - } - else - { + } else { BYTE* p = (BYTE*)memPtr; p[0] = (BYTE)val; p[1] = (BYTE)(val>>8); @@ -210,8 +206,7 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr) { if (MEM_isLittleEndian()) return MEM_read32(memPtr); - else - { + else { const BYTE* p = (const BYTE*)memPtr; return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24)); } @@ -219,12 +214,9 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr) MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32) { - if (MEM_isLittleEndian()) - { + if (MEM_isLittleEndian()) { MEM_write32(memPtr, val32); - } - else - { + } else { BYTE* p = (BYTE*)memPtr; p[0] = (BYTE)val32; p[1] = (BYTE)(val32>>8); @@ -237,8 +229,7 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr) { if (MEM_isLittleEndian()) return MEM_read64(memPtr); - else - { + else { const BYTE* p = (const BYTE*)memPtr; return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24) + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56)); @@ -247,12 +238,9 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr) MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64) { - if (MEM_isLittleEndian()) - { + if (MEM_isLittleEndian()) { MEM_write64(memPtr, val64); - } - else - { + } else { BYTE* p = (BYTE*)memPtr; p[0] = (BYTE)val64; p[1] = (BYTE)(val64>>8); diff --git a/lib/zstd_buffered.c b/lib/zstd_buffered.c index 2a8df235..133c54e1 100644 --- a/lib/zstd_buffered.c +++ b/lib/zstd_buffered.c @@ -36,7 +36,7 @@ */ /* ************************************* -* Includes +* Dependencies ***************************************/ #include #include "error_private.h" @@ -44,6 +44,12 @@ #include "zstd_buffered_static.h" +/* ************************************* +* Constants +***************************************/ +static size_t ZBUFF_blockHeaderSize = 3; +static size_t ZBUFF_endFrameSize = 3; + /** ************************************************ * Streaming compression * @@ -522,7 +528,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePt { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbc->zc); - if (nextSrcSizeHint > 3) nextSrcSizeHint+= 3; /* get the next block header while at it */ + if (nextSrcSizeHint > ZBUFF_blockHeaderSize) nextSrcSizeHint+= ZBUFF_blockHeaderSize; /* get next block header too */ nextSrcSizeHint -= zbc->inPos; /* already loaded*/ return nextSrcSizeHint; } @@ -536,7 +542,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePt unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); } const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } -size_t ZBUFF_recommendedCInSize() { return BLOCKSIZE; } -size_t ZBUFF_recommendedCOutSize() { return ZSTD_compressBound(BLOCKSIZE) + 6; } -size_t ZBUFF_recommendedDInSize() { return BLOCKSIZE + 3; } -size_t ZBUFF_recommendedDOutSize() { return BLOCKSIZE; } +size_t ZBUFF_recommendedCInSize(void) { return BLOCKSIZE; } +size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(BLOCKSIZE) + ZBUFF_blockHeaderSize + ZBUFF_endFrameSize; } +size_t ZBUFF_recommendedDInSize(void) { return BLOCKSIZE + ZBUFF_blockHeaderSize /* block header size*/ ; } +size_t ZBUFF_recommendedDOutSize(void) { return BLOCKSIZE; } diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 6684bae4..f4ddb910 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -49,14 +49,13 @@ /* ************************************* -* Includes +* Dependencies ***************************************/ #include /* malloc */ #include /* memset */ #include "mem.h" #include "fse_static.h" #include "huff0_static.h" -#include "zstd_static.h" #include "zstd_internal.h" @@ -2260,7 +2259,6 @@ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSi /*- Pre-defined compression levels -*/ -#define ZSTD_MAX_CLEVEL 20 unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index 5d637d2b..1a724489 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -1,6 +1,6 @@ /* zstd - standard compression library - Copyright (C) 2014-2015, Yann Collet. + Copyright (C) 2014-2016, Yann Collet. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) @@ -27,7 +27,6 @@ You can contact the author at : - zstd source repository : https://github.com/Cyan4973/zstd - - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c */ /* *************************************************************** @@ -44,7 +43,7 @@ /*! * LEGACY_SUPPORT : -* ZSTD_decompress() can decode older formats (v0.1+) if set to 1 +* if set to 1, ZSTD_decompress() can decode older formats (v0.1+) */ #ifndef ZSTD_LEGACY_SUPPORT # define ZSTD_LEGACY_SUPPORT 0 @@ -58,7 +57,6 @@ #include /* memcpy, memmove */ #include /* debug : printf */ #include "mem.h" /* low level memory routines */ -#include "zstd_static.h" #include "zstd_internal.h" #include "fse_static.h" #include "huff0_static.h" diff --git a/lib/zstd_static.h b/lib/zstd_static.h index 50f301fa..49a5e3de 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -82,6 +82,7 @@ typedef struct /* ************************************* * Advanced functions ***************************************/ +#define ZSTD_MAX_CLEVEL 20 ZSTDLIB_API unsigned ZSTD_maxCLevel (void); /*! ZSTD_getParams