at v5.13 273 lines 8.9 kB view raw
1/** 2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of https://github.com/facebook/zstd. 7 * An additional grant of patent rights can be found in the PATENTS file in the 8 * same directory. 9 * 10 * This program is free software; you can redistribute it and/or modify it under 11 * the terms of the GNU General Public License version 2 as published by the 12 * Free Software Foundation. This program is dual-licensed; you may select 13 * either version 2 of the GNU General Public License ("GPL") or BSD license 14 * ("BSD"). 15 */ 16 17#ifndef ZSTD_CCOMMON_H_MODULE 18#define ZSTD_CCOMMON_H_MODULE 19 20/*-******************************************************* 21* Compiler specifics 22*********************************************************/ 23#define FORCE_INLINE static __always_inline 24#define FORCE_NOINLINE static noinline 25 26/*-************************************* 27* Dependencies 28***************************************/ 29#include "error_private.h" 30#include "mem.h" 31#include <linux/compiler.h> 32#include <linux/kernel.h> 33#include <linux/xxhash.h> 34#include <linux/zstd.h> 35 36/*-************************************* 37* shared macros 38***************************************/ 39#define MIN(a, b) ((a) < (b) ? (a) : (b)) 40#define MAX(a, b) ((a) > (b) ? (a) : (b)) 41#define CHECK_F(f) \ 42 { \ 43 size_t const errcod = f; \ 44 if (ERR_isError(errcod)) \ 45 return errcod; \ 46 } /* check and Forward error code */ 47#define CHECK_E(f, e) \ 48 { \ 49 size_t const errcod = f; \ 50 if (ERR_isError(errcod)) \ 51 return ERROR(e); \ 52 } /* check and send Error code */ 53#define ZSTD_STATIC_ASSERT(c) \ 54 { \ 55 enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \ 56 } 57 58/*-************************************* 59* Common constants 60***************************************/ 61#define ZSTD_OPT_NUM (1 << 12) 62#define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */ 63 64#define ZSTD_REP_NUM 3 /* number of repcodes */ 65#define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */ 66#define ZSTD_REP_MOVE (ZSTD_REP_NUM - 1) 67#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM) 68static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8}; 69 70#define KB *(1 << 10) 71#define MB *(1 << 20) 72#define GB *(1U << 30) 73 74#define BIT7 128 75#define BIT6 64 76#define BIT5 32 77#define BIT4 16 78#define BIT1 2 79#define BIT0 1 80 81#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10 82static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8}; 83static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4}; 84 85#define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */ 86static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; 87typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e; 88 89#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ 90#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ 91 92#define HufLog 12 93typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e; 94 95#define LONGNBSEQ 0x7F00 96 97#define MINMATCH 3 98#define EQUAL_READ32 4 99 100#define Litbits 8 101#define MaxLit ((1 << Litbits) - 1) 102#define MaxML 52 103#define MaxLL 35 104#define MaxOff 28 105#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ 106#define MLFSELog 9 107#define LLFSELog 9 108#define OffFSELog 8 109 110static const U32 LL_bits[MaxLL + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 111static const S16 LL_defaultNorm[MaxLL + 1] = {4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, -1, -1, -1, -1}; 112#define LL_DEFAULTNORMLOG 6 /* for static allocation */ 113static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG; 114 115static const U32 ML_bits[MaxML + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 117static const S16 ML_defaultNorm[MaxML + 1] = {1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1}; 119#define ML_DEFAULTNORMLOG 6 /* for static allocation */ 120static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG; 121 122static const S16 OF_defaultNorm[MaxOff + 1] = {1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1}; 123#define OF_DEFAULTNORMLOG 5 /* for static allocation */ 124static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG; 125 126/*-******************************************* 127* Shared functions to include for inlining 128*********************************************/ 129ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) { 130 /* 131 * zstd relies heavily on gcc being able to analyze and inline this 132 * memcpy() call, since it is called in a tight loop. Preboot mode 133 * is compiled in freestanding mode, which stops gcc from analyzing 134 * memcpy(). Use __builtin_memcpy() to tell gcc to analyze this as a 135 * regular memcpy(). 136 */ 137 __builtin_memcpy(dst, src, 8); 138} 139/*! ZSTD_wildcopy() : 140* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ 141#define WILDCOPY_OVERLENGTH 8 142ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length) 143{ 144 const BYTE* ip = (const BYTE*)src; 145 BYTE* op = (BYTE*)dst; 146 BYTE* const oend = op + length; 147#if defined(GCC_VERSION) && GCC_VERSION >= 70000 && GCC_VERSION < 70200 148 /* 149 * Work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81388. 150 * Avoid the bad case where the loop only runs once by handling the 151 * special case separately. This doesn't trigger the bug because it 152 * doesn't involve pointer/integer overflow. 153 */ 154 if (length <= 8) 155 return ZSTD_copy8(dst, src); 156#endif 157 do { 158 ZSTD_copy8(op, ip); 159 op += 8; 160 ip += 8; 161 } while (op < oend); 162} 163 164/*-******************************************* 165* Private interfaces 166*********************************************/ 167typedef struct ZSTD_stats_s ZSTD_stats_t; 168 169typedef struct { 170 U32 off; 171 U32 len; 172} ZSTD_match_t; 173 174typedef struct { 175 U32 price; 176 U32 off; 177 U32 mlen; 178 U32 litlen; 179 U32 rep[ZSTD_REP_NUM]; 180} ZSTD_optimal_t; 181 182typedef struct seqDef_s { 183 U32 offset; 184 U16 litLength; 185 U16 matchLength; 186} seqDef; 187 188typedef struct { 189 seqDef *sequencesStart; 190 seqDef *sequences; 191 BYTE *litStart; 192 BYTE *lit; 193 BYTE *llCode; 194 BYTE *mlCode; 195 BYTE *ofCode; 196 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */ 197 U32 longLengthPos; 198 /* opt */ 199 ZSTD_optimal_t *priceTable; 200 ZSTD_match_t *matchTable; 201 U32 *matchLengthFreq; 202 U32 *litLengthFreq; 203 U32 *litFreq; 204 U32 *offCodeFreq; 205 U32 matchLengthSum; 206 U32 matchSum; 207 U32 litLengthSum; 208 U32 litSum; 209 U32 offCodeSum; 210 U32 log2matchLengthSum; 211 U32 log2matchSum; 212 U32 log2litLengthSum; 213 U32 log2litSum; 214 U32 log2offCodeSum; 215 U32 factor; 216 U32 staticPrices; 217 U32 cachedPrice; 218 U32 cachedLitLength; 219 const BYTE *cachedLiterals; 220} seqStore_t; 221 222const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx); 223void ZSTD_seqToCodes(const seqStore_t *seqStorePtr); 224int ZSTD_isSkipFrame(ZSTD_DCtx *dctx); 225 226/*= Custom memory allocation functions */ 227typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size); 228typedef void (*ZSTD_freeFunction)(void *opaque, void *address); 229typedef struct { 230 ZSTD_allocFunction customAlloc; 231 ZSTD_freeFunction customFree; 232 void *opaque; 233} ZSTD_customMem; 234 235void *ZSTD_malloc(size_t size, ZSTD_customMem customMem); 236void ZSTD_free(void *ptr, ZSTD_customMem customMem); 237 238/*====== stack allocation ======*/ 239 240typedef struct { 241 void *ptr; 242 const void *end; 243} ZSTD_stack; 244 245#define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t)) 246#define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t)) 247 248ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize); 249 250void *ZSTD_stackAllocAll(void *opaque, size_t *size); 251void *ZSTD_stackAlloc(void *opaque, size_t size); 252void ZSTD_stackFree(void *opaque, void *address); 253 254/*====== common function ======*/ 255 256ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); } 257 258/* hidden functions */ 259 260/* ZSTD_invalidateRepCodes() : 261 * ensures next compression will not use repcodes from previous block. 262 * Note : only works with regular variant; 263 * do not use with extDict variant ! */ 264void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx); 265 266size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx); 267size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx); 268size_t ZSTD_freeCDict(ZSTD_CDict *cdict); 269size_t ZSTD_freeDDict(ZSTD_DDict *cdict); 270size_t ZSTD_freeCStream(ZSTD_CStream *zcs); 271size_t ZSTD_freeDStream(ZSTD_DStream *zds); 272 273#endif /* ZSTD_CCOMMON_H_MODULE */