2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
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
17 #ifndef ZSTD_CCOMMON_H_MODULE
18 #define ZSTD_CCOMMON_H_MODULE
20 /*-*******************************************************
22 *********************************************************/
23 #define FORCE_INLINE static __always_inline
24 #define FORCE_NOINLINE static noinline
26 /*-*************************************
28 ***************************************/
29 #include "error_private.h"
31 #include <linux/compiler.h>
32 #include <linux/kernel.h>
33 #include <linux/xxhash.h>
34 #include <linux/zstd.h>
36 /*-*************************************
38 ***************************************/
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 #define MAX(a, b) ((a) > (b) ? (a) : (b))
43 size_t const errcod = f; \
44 if (ERR_isError(errcod)) \
46 } /* check and Forward error code */
47 #define CHECK_E(f, e) \
49 size_t const errcod = f; \
50 if (ERR_isError(errcod)) \
52 } /* check and send Error code */
53 #define ZSTD_STATIC_ASSERT(c) \
55 enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \
58 /*-*************************************
60 ***************************************/
61 #define ZSTD_OPT_NUM (1 << 12)
62 #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
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)
68 static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8};
72 #define GB *(1U << 30)
81 #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
82 static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8};
83 static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4};
85 #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
86 static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
87 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
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 */
93 typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
95 #define LONGNBSEQ 0x7F00
98 #define EQUAL_READ32 4
101 #define MaxLit ((1 << Litbits) - 1)
105 #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
110 static 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};
111 static 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 */
113 static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
115 static 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};
117 static 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 */
120 static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
122 static 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 */
124 static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
126 /*-*******************************************
127 * Shared functions to include for inlining
128 *********************************************/
129 ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) {
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
137 __builtin_memcpy(dst, src, 8);
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
142 ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length)
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
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.
155 return ZSTD_copy8(dst, src);
164 /*-*******************************************
166 *********************************************/
167 typedef struct ZSTD_stats_s ZSTD_stats_t;
179 U32 rep[ZSTD_REP_NUM];
182 typedef struct seqDef_s {
189 seqDef *sequencesStart;
196 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
199 ZSTD_optimal_t *priceTable;
200 ZSTD_match_t *matchTable;
201 U32 *matchLengthFreq;
210 U32 log2matchLengthSum;
212 U32 log2litLengthSum;
219 const BYTE *cachedLiterals;
222 const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx);
223 void ZSTD_seqToCodes(const seqStore_t *seqStorePtr);
224 int ZSTD_isSkipFrame(ZSTD_DCtx *dctx);
226 /*= Custom memory allocation functions */
227 typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size);
228 typedef void (*ZSTD_freeFunction)(void *opaque, void *address);
230 ZSTD_allocFunction customAlloc;
231 ZSTD_freeFunction customFree;
235 void *ZSTD_malloc(size_t size, ZSTD_customMem customMem);
236 void ZSTD_free(void *ptr, ZSTD_customMem customMem);
238 /*====== stack allocation ======*/
245 #define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t))
246 #define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t))
248 ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize);
250 void *ZSTD_stackAllocAll(void *opaque, size_t *size);
251 void *ZSTD_stackAlloc(void *opaque, size_t size);
252 void ZSTD_stackFree(void *opaque, void *address);
254 /*====== common function ======*/
256 ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); }
258 /* hidden functions */
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 ! */
264 void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx);
266 size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx);
267 size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx);
268 size_t ZSTD_freeCDict(ZSTD_CDict *cdict);
269 size_t ZSTD_freeDDict(ZSTD_DDict *cdict);
270 size_t ZSTD_freeCStream(ZSTD_CStream *zcs);
271 size_t ZSTD_freeDStream(ZSTD_DStream *zds);
273 #endif /* ZSTD_CCOMMON_H_MODULE */