zstd_internal.h 8.3 KB

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