zstd_internal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* SPDX-License-Identifier: (GPL-2.0 or BSD-3-Clause-Clear) */
  2. /**
  3. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  4. * All rights reserved.
  5. */
  6. #ifndef ZSTD_CCOMMON_H_MODULE
  7. #define ZSTD_CCOMMON_H_MODULE
  8. /*-*******************************************************
  9. * Compiler specifics
  10. *********************************************************/
  11. #define FORCE_INLINE static __always_inline
  12. #define FORCE_NOINLINE static noinline
  13. /*-*************************************
  14. * Dependencies
  15. ***************************************/
  16. #include "error_private.h"
  17. #include "mem.h"
  18. #include <linux/compiler.h>
  19. #include <linux/kernel.h>
  20. #include <linux/xxhash.h>
  21. #include <linux/zstd.h>
  22. /*-*************************************
  23. * shared macros
  24. ***************************************/
  25. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  26. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  27. #define CHECK_F(f) \
  28. { \
  29. size_t const errcod = f; \
  30. if (ERR_isError(errcod)) \
  31. return errcod; \
  32. } /* check and Forward error code */
  33. #define CHECK_E(f, e) \
  34. { \
  35. size_t const errcod = f; \
  36. if (ERR_isError(errcod)) \
  37. return ERROR(e); \
  38. } /* check and send Error code */
  39. #define ZSTD_STATIC_ASSERT(c) \
  40. { \
  41. enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \
  42. }
  43. /*-*************************************
  44. * Common constants
  45. ***************************************/
  46. #define ZSTD_OPT_NUM (1 << 12)
  47. #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
  48. #define ZSTD_REP_NUM 3 /* number of repcodes */
  49. #define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
  50. #define ZSTD_REP_MOVE (ZSTD_REP_NUM - 1)
  51. #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
  52. static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8};
  53. #define KB *(1 << 10)
  54. #define MB *(1 << 20)
  55. #define GB *(1U << 30)
  56. #define BIT7 128
  57. #define BIT6 64
  58. #define BIT5 32
  59. #define BIT4 16
  60. #define BIT1 2
  61. #define BIT0 1
  62. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  63. static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8};
  64. static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4};
  65. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  66. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  67. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  68. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  69. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  70. #define HufLog 12
  71. typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
  72. #define LONGNBSEQ 0x7F00
  73. #define MINMATCH 3
  74. #define EQUAL_READ32 4
  75. #define Litbits 8
  76. #define MaxLit ((1 << Litbits) - 1)
  77. #define MaxML 52
  78. #define MaxLL 35
  79. #define MaxOff 28
  80. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  81. #define MLFSELog 9
  82. #define LLFSELog 9
  83. #define OffFSELog 8
  84. 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};
  85. 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};
  86. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  87. static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  88. 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,
  89. 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};
  90. 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,
  91. 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};
  92. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  93. static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  94. 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};
  95. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  96. static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  97. /*-*******************************************
  98. * Shared functions to include for inlining
  99. *********************************************/
  100. ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) {
  101. memcpy(dst, src, 8);
  102. }
  103. /*! ZSTD_wildcopy() :
  104. * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
  105. #define WILDCOPY_OVERLENGTH 8
  106. ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length)
  107. {
  108. const BYTE* ip = (const BYTE*)src;
  109. BYTE* op = (BYTE*)dst;
  110. BYTE* const oend = op + length;
  111. /* Work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81388.
  112. * Avoid the bad case where the loop only runs once by handling the
  113. * special case separately. This doesn't trigger the bug because it
  114. * doesn't involve pointer/integer overflow.
  115. */
  116. if (length <= 8)
  117. return ZSTD_copy8(dst, src);
  118. do {
  119. ZSTD_copy8(op, ip);
  120. op += 8;
  121. ip += 8;
  122. } while (op < oend);
  123. }
  124. /*-*******************************************
  125. * Private interfaces
  126. *********************************************/
  127. typedef struct ZSTD_stats_s ZSTD_stats_t;
  128. typedef struct {
  129. U32 off;
  130. U32 len;
  131. } ZSTD_match_t;
  132. typedef struct {
  133. U32 price;
  134. U32 off;
  135. U32 mlen;
  136. U32 litlen;
  137. U32 rep[ZSTD_REP_NUM];
  138. } ZSTD_optimal_t;
  139. typedef struct seqDef_s {
  140. U32 offset;
  141. U16 litLength;
  142. U16 matchLength;
  143. } seqDef;
  144. typedef struct {
  145. seqDef *sequencesStart;
  146. seqDef *sequences;
  147. BYTE *litStart;
  148. BYTE *lit;
  149. BYTE *llCode;
  150. BYTE *mlCode;
  151. BYTE *ofCode;
  152. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  153. U32 longLengthPos;
  154. /* opt */
  155. ZSTD_optimal_t *priceTable;
  156. ZSTD_match_t *matchTable;
  157. U32 *matchLengthFreq;
  158. U32 *litLengthFreq;
  159. U32 *litFreq;
  160. U32 *offCodeFreq;
  161. U32 matchLengthSum;
  162. U32 matchSum;
  163. U32 litLengthSum;
  164. U32 litSum;
  165. U32 offCodeSum;
  166. U32 log2matchLengthSum;
  167. U32 log2matchSum;
  168. U32 log2litLengthSum;
  169. U32 log2litSum;
  170. U32 log2offCodeSum;
  171. U32 factor;
  172. U32 staticPrices;
  173. U32 cachedPrice;
  174. U32 cachedLitLength;
  175. const BYTE *cachedLiterals;
  176. } seqStore_t;
  177. const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx);
  178. void ZSTD_seqToCodes(const seqStore_t *seqStorePtr);
  179. int ZSTD_isSkipFrame(ZSTD_DCtx *dctx);
  180. /*= Custom memory allocation functions */
  181. typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size);
  182. typedef void (*ZSTD_freeFunction)(void *opaque, void *address);
  183. typedef struct {
  184. ZSTD_allocFunction customAlloc;
  185. ZSTD_freeFunction customFree;
  186. void *opaque;
  187. } ZSTD_customMem;
  188. void *ZSTD_malloc(size_t size, ZSTD_customMem customMem);
  189. void ZSTD_free(void *ptr, ZSTD_customMem customMem);
  190. /*====== stack allocation ======*/
  191. typedef struct {
  192. void *ptr;
  193. const void *end;
  194. } ZSTD_stack;
  195. #define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t))
  196. #define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t))
  197. ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize);
  198. void *ZSTD_stackAllocAll(void *opaque, size_t *size);
  199. void *ZSTD_stackAlloc(void *opaque, size_t size);
  200. void ZSTD_stackFree(void *opaque, void *address);
  201. /*====== common function ======*/
  202. ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); }
  203. /* hidden functions */
  204. /* ZSTD_invalidateRepCodes() :
  205. * ensures next compression will not use repcodes from previous block.
  206. * Note : only works with regular variant;
  207. * do not use with extDict variant ! */
  208. void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx);
  209. size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx);
  210. size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx);
  211. size_t ZSTD_freeCDict(ZSTD_CDict *cdict);
  212. size_t ZSTD_freeDDict(ZSTD_DDict *cdict);
  213. size_t ZSTD_freeCStream(ZSTD_CStream *zcs);
  214. size_t ZSTD_freeDStream(ZSTD_DStream *zds);
  215. #endif /* ZSTD_CCOMMON_H_MODULE */