Changeset View
Changeset View
Standalone View
Standalone View
contrib/zstd/lib/common/fse_decompress.c
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
# define FORCE_INLINE static | # define FORCE_INLINE static | ||||
# endif /* __STDC_VERSION__ */ | # endif /* __STDC_VERSION__ */ | ||||
#endif | #endif | ||||
/* ************************************************************** | /* ************************************************************** | ||||
* Includes | * Includes | ||||
****************************************************************/ | ****************************************************************/ | ||||
#ifdef _KERNEL | |||||
#include <sys/systm.h> /* memset */ | |||||
#else | |||||
#include <stdlib.h> /* malloc, free, qsort */ | #include <stdlib.h> /* malloc, free, qsort */ | ||||
imp: I wonder why not have a zstd_kernel.h and put all this stuff in there.
Then you could use the… | |||||
#include <string.h> /* memcpy, memset */ | #include <string.h> /* memcpy, memset */ | ||||
#endif | |||||
Done Inline ActionsWhy _BOOTSTRAP? imp: Why _BOOTSTRAP? | |||||
#include "bitstream.h" | #include "bitstream.h" | ||||
#define FSE_STATIC_LINKING_ONLY | #define FSE_STATIC_LINKING_ONLY | ||||
#include "fse.h" | #include "fse.h" | ||||
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) | |||||
#include "zstd_internal.h" /* defaultCustomMem */ | |||||
static ZSTD_customMem customMalloc = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL }; | |||||
#endif | |||||
/* ************************************************************** | /* ************************************************************** | ||||
* Error Management | * Error Management | ||||
****************************************************************/ | ****************************************************************/ | ||||
#define FSE_isError ERR_isError | #define FSE_isError ERR_isError | ||||
#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ | #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ | ||||
/* check and forward error code */ | /* check and forward error code */ | ||||
#define CHECK_F(f) { size_t const e = f; if (FSE_isError(e)) return e; } | #define CHECK_FSE_F(f) { size_t const e = f; if (FSE_isError(e)) return e; } | ||||
/* ************************************************************** | /* ************************************************************** | ||||
* Templates | * Templates | ||||
****************************************************************/ | ****************************************************************/ | ||||
/* | /* | ||||
designed to be included | designed to be included | ||||
for type-specific functions (template emulation in C) | for type-specific functions (template emulation in C) | ||||
Show All 10 Lines | |||||
/* Function names */ | /* Function names */ | ||||
#define FSE_CAT(X,Y) X##Y | #define FSE_CAT(X,Y) X##Y | ||||
#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) | #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) | ||||
#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) | #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) | ||||
/* Function templates */ | /* Function templates */ | ||||
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) | |||||
FSE_DTable* FSE_createDTable (unsigned tableLog) | FSE_DTable* FSE_createDTable (unsigned tableLog) | ||||
{ | { | ||||
if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX; | if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX; | ||||
return (FSE_DTable*)ZSTD_malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32), customMalloc ); | |||||
} | |||||
void FSE_freeDTable (FSE_DTable* dt) | |||||
{ | |||||
ZSTD_free(dt, customMalloc); | |||||
} | |||||
#else | |||||
FSE_DTable* FSE_createDTable (unsigned tableLog) | |||||
{ | |||||
if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX; | |||||
return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) ); | return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) ); | ||||
} | } | ||||
Done Inline ActionsShould this be WAITOK? cem: Should this be WAITOK? | |||||
Done Inline ActionsSeems like debugging cruft got added here cem: Seems like debugging cruft got added here | |||||
Done Inline ActionsIf you create a malloc wrapper, you'd have fewer ifdefs. imp: If you create a malloc wrapper, you'd have fewer ifdefs. | |||||
void FSE_freeDTable (FSE_DTable* dt) | void FSE_freeDTable (FSE_DTable* dt) | ||||
{ | { | ||||
free(dt); | free(dt); | ||||
} | } | ||||
#endif | |||||
size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) | size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) | ||||
{ | { | ||||
void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ | void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ | ||||
FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); | FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); | ||||
U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; | U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; | ||||
U32 const maxSV1 = maxSymbolValue + 1; | U32 const maxSV1 = maxSymbolValue + 1; | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | FORCE_INLINE size_t FSE_decompress_usingDTable_generic( | ||||
BYTE* const omax = op + maxDstSize; | BYTE* const omax = op + maxDstSize; | ||||
BYTE* const olimit = omax-3; | BYTE* const olimit = omax-3; | ||||
BIT_DStream_t bitD; | BIT_DStream_t bitD; | ||||
FSE_DState_t state1; | FSE_DState_t state1; | ||||
FSE_DState_t state2; | FSE_DState_t state2; | ||||
/* Init */ | /* Init */ | ||||
CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); | CHECK_FSE_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); | ||||
FSE_initDState(&state1, &bitD, dt); | FSE_initDState(&state1, &bitD, dt); | ||||
FSE_initDState(&state2, &bitD, dt); | FSE_initDState(&state2, &bitD, dt); | ||||
#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) | #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) | ||||
/* 4 symbols per loop */ | /* 4 symbols per loop */ | ||||
for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) { | for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) { | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog) | ||||
/* normal FSE decoding mode */ | /* normal FSE decoding mode */ | ||||
size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); | size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); | ||||
if (FSE_isError(NCountLength)) return NCountLength; | if (FSE_isError(NCountLength)) return NCountLength; | ||||
//if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */ | //if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */ | ||||
if (tableLog > maxLog) return ERROR(tableLog_tooLarge); | if (tableLog > maxLog) return ERROR(tableLog_tooLarge); | ||||
ip += NCountLength; | ip += NCountLength; | ||||
cSrcSize -= NCountLength; | cSrcSize -= NCountLength; | ||||
CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) ); | CHECK_FSE_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) ); | ||||
return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */ | return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */ | ||||
} | } | ||||
typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; | typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; | ||||
size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize) | size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize) | ||||
{ | { | ||||
DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ | DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ | ||||
return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG); | return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG); | ||||
} | } | ||||
#endif /* FSE_COMMONDEFS_ONLY */ | #endif /* FSE_COMMONDEFS_ONLY */ |
I wonder why not have a zstd_kernel.h and put all this stuff in there.
Then you could use the libsa trick of using sed to create the right sources and not have to put a bunch of ifdefs in?