lib/zstd/compress/huf_compress.c
Source file repositories/reference/linux-study-clean/lib/zstd/compress/huf_compress.c
File Facts
- System
- Linux kernel
- Corpus path
lib/zstd/compress/huf_compress.c- Extension
.c- Size
- 58448 bytes
- Lines
- 1463
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
../common/zstd_deps.h../common/compiler.h../common/bitstream.hhist.h../common/fse.h../common/huf.h../common/error_private.h../common/bits.h
Detected Declarations
function showU32function showCTableBitsfunction showHNodeSymbolsfunction showHNodeBitsfunction HUF_alignUpWorkspacefunction HUF_compressWeightsfunction HUF_getNbBitsfunction HUF_getNbBitsFastfunction HUF_getValuefunction HUF_getValueFastfunction HUF_setNbBitsfunction HUF_setValuefunction HUF_readCTableHeaderfunction HUF_writeCTableHeaderfunction HUF_writeCTable_wkspfunction HUF_readCTablefunction HUF_getNbBitsFromCTablefunction HUF_setMaxHeightfunction HUF_getIndexfunction HUF_swapNodesfunction HUF_isSortedfunction HUF_insertionSortfunction HUF_quickSortPartitionfunction HUF_simpleQuickSortfunction HUF_sortfunction HUF_buildCTablefunction HUF_buildCTableFromTreefunction HUF_buildCTable_wkspfunction HUF_estimateCompressedSizefunction HUF_validateCTablefunction HUF_compressBoundfunction HUF_initCStreamfunction HUF_addBitsfunction HUF_zeroIndex1function HUF_mergeIndex1function HUF_flushBitsfunction HUF_endMarkfunction HUF_closeCStreamfunction HUF_encodeSymbolfunction HUF_compress1X_usingCTable_internal_body_loopfunction HUF_tightCompressBoundfunction HUF_compress1X_usingCTable_internal_bodyfunction HUF_compress1X_usingCTable_internal_bmi2function HUF_compress1X_usingCTable_internal_defaultfunction HUF_compress1X_usingCTable_internalfunction HUF_compress1X_usingCTable_internalfunction HUF_compress1X_usingCTablefunction HUF_compress4X_usingCTable_internal
Annotated Snippet
if (*workspaceSizePtr >= add) {
assert(add < align);
assert(((size_t)aligned & mask) == 0);
*workspaceSizePtr -= add;
return aligned;
} else {
*workspaceSizePtr = 0;
return NULL;
}
}
/* HUF_compressWeights() :
* Same as FSE_compress(), but dedicated to huff0's weights compression.
* The use case needs much less stack memory.
* Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
*/
#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
typedef struct {
FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)];
U32 scratchBuffer[FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(HUF_TABLELOG_MAX, MAX_FSE_TABLELOG_FOR_HUFF_HEADER)];
unsigned count[HUF_TABLELOG_MAX+1];
S16 norm[HUF_TABLELOG_MAX+1];
} HUF_CompressWeightsWksp;
static size_t
HUF_compressWeights(void* dst, size_t dstSize,
const void* weightTable, size_t wtSize,
void* workspace, size_t workspaceSize)
{
BYTE* const ostart = (BYTE*) dst;
BYTE* op = ostart;
BYTE* const oend = ostart + dstSize;
unsigned maxSymbolValue = HUF_TABLELOG_MAX;
U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER;
HUF_CompressWeightsWksp* wksp = (HUF_CompressWeightsWksp*)HUF_alignUpWorkspace(workspace, &workspaceSize, ZSTD_ALIGNOF(U32));
if (workspaceSize < sizeof(HUF_CompressWeightsWksp)) return ERROR(GENERIC);
/* init conditions */
if (wtSize <= 1) return 0; /* Not compressible */
/* Scan input and build symbol stats */
{ unsigned const maxCount = HIST_count_simple(wksp->count, &maxSymbolValue, weightTable, wtSize); /* never fails */
if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */
if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */
}
tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
CHECK_F( FSE_normalizeCount(wksp->norm, tableLog, wksp->count, wtSize, maxSymbolValue, /* useLowProbCount */ 0) );
/* Write table description header */
{ CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), wksp->norm, maxSymbolValue, tableLog) );
op += hSize;
}
/* Compress */
CHECK_F( FSE_buildCTable_wksp(wksp->CTable, wksp->norm, maxSymbolValue, tableLog, wksp->scratchBuffer, sizeof(wksp->scratchBuffer)) );
{ CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, wksp->CTable) );
if (cSize == 0) return 0; /* not enough space for compressed data */
op += cSize;
}
return (size_t)(op-ostart);
}
static size_t HUF_getNbBits(HUF_CElt elt)
{
return elt & 0xFF;
}
static size_t HUF_getNbBitsFast(HUF_CElt elt)
{
return elt;
}
static size_t HUF_getValue(HUF_CElt elt)
{
return elt & ~(size_t)0xFF;
}
static size_t HUF_getValueFast(HUF_CElt elt)
{
return elt;
}
static void HUF_setNbBits(HUF_CElt* elt, size_t nbBits)
{
Annotation
- Immediate include surface: `../common/zstd_deps.h`, `../common/compiler.h`, `../common/bitstream.h`, `hist.h`, `../common/fse.h`, `../common/huf.h`, `../common/error_private.h`, `../common/bits.h`.
- Detected declarations: `function showU32`, `function showCTableBits`, `function showHNodeSymbols`, `function showHNodeBits`, `function HUF_alignUpWorkspace`, `function HUF_compressWeights`, `function HUF_getNbBits`, `function HUF_getNbBitsFast`, `function HUF_getValue`, `function HUF_getValueFast`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.