zstdlib 0.6.0-x86-mingw32 → 0.9.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +20 -0
  3. data/README.md +7 -1
  4. data/Rakefile +38 -8
  5. data/ext/{zstdlib → zstdlib_c}/extconf.rb +10 -5
  6. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.2/zstdlib.c +2 -2
  7. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.3/zstdlib.c +2 -2
  8. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.4/zstdlib.c +2 -2
  9. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.5/zstdlib.c +2 -2
  10. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.6/zstdlib.c +2 -2
  11. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.7/zstdlib.c +2 -2
  12. data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
  13. data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
  14. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/adler32.c +0 -0
  15. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/compress.c +0 -0
  16. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/crc32.c +0 -0
  17. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/crc32.h +0 -0
  18. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/deflate.c +0 -0
  19. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/deflate.h +0 -0
  20. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzclose.c +0 -0
  21. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzguts.h +0 -0
  22. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzlib.c +0 -0
  23. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzread.c +0 -0
  24. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzwrite.c +0 -0
  25. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/infback.c +0 -0
  26. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffast.c +0 -0
  27. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffast.h +0 -0
  28. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffixed.h +0 -0
  29. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inflate.c +0 -0
  30. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inflate.h +0 -0
  31. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inftrees.c +0 -0
  32. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inftrees.h +0 -0
  33. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/trees.c +0 -0
  34. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/trees.h +0 -0
  35. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/uncompr.c +0 -0
  36. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zconf.h +0 -0
  37. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zlib.h +0 -0
  38. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zutil.c +0 -0
  39. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zutil.h +0 -0
  40. data/ext/{zstdlib → zstdlib_c}/zlib.mk +0 -0
  41. data/ext/{zstdlib → zstdlib_c}/zlibwrapper/zlibwrapper.c +1 -5
  42. data/ext/{zstdlib → zstdlib_c}/zlibwrapper.mk +0 -0
  43. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/bitstream.h +75 -57
  44. data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
  45. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/cpu.h +1 -3
  46. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.c +24 -0
  47. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/debug.h +22 -49
  48. data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
  49. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/error_private.c +3 -1
  50. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
  51. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/fse.h +51 -42
  52. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/fse_decompress.c +149 -57
  53. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/huf.h +60 -54
  54. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/mem.h +87 -98
  55. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/pool.c +34 -23
  56. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/pool.h +5 -5
  57. data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
  58. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/threading.c +10 -8
  59. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/threading.h +4 -3
  60. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
  61. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
  62. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/zstd_common.c +10 -10
  63. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
  64. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_internal.h +493 -0
  65. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
  66. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
  67. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/fse_compress.c +105 -85
  68. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.c +41 -63
  69. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.h +13 -33
  70. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
  71. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress.c +6327 -0
  72. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_internal.h +537 -82
  73. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.c +21 -16
  74. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.h +4 -2
  75. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.c +61 -34
  76. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.h +10 -3
  77. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.c +573 -0
  78. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.h +32 -0
  79. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_cwksp.h +236 -95
  80. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.c +321 -143
  81. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.h +2 -2
  82. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.c +328 -137
  83. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.h +2 -2
  84. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
  85. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
  86. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.c +336 -209
  87. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.h +15 -3
  88. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
  89. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.c +439 -239
  90. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.h +1 -1
  91. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstdmt_compress.c +205 -462
  92. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
  93. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress.c +1889 -0
  94. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
  95. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.c +20 -16
  96. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.h +3 -3
  97. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress.c +691 -230
  98. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.c +2072 -0
  99. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_block.h +16 -7
  100. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_internal.h +71 -10
  101. data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
  102. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/zstd.h +760 -234
  103. data/ext/{zstdlib/zstd-1.4.4/lib/common → zstdlib_c/zstd-1.5.2/lib}/zstd_errors.h +3 -1
  104. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzclose.c +0 -0
  105. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzcompatibility.h +1 -1
  106. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzguts.h +0 -0
  107. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzlib.c +0 -0
  108. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzread.c +0 -0
  109. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzwrite.c +0 -0
  110. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.c +133 -44
  111. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.h +1 -1
  112. data/ext/zstdlib_c/zstd.mk +15 -0
  113. data/lib/2.4/zstdlib_c.so +0 -0
  114. data/lib/2.5/zstdlib_c.so +0 -0
  115. data/lib/2.6/zstdlib_c.so +0 -0
  116. data/lib/2.7/zstdlib_c.so +0 -0
  117. data/lib/3.0/zstdlib_c.so +0 -0
  118. data/lib/3.1/zstdlib_c.so +0 -0
  119. data/lib/zstdlib.rb +2 -2
  120. metadata +125 -114
  121. data/ext/zstdlib/zstd-1.4.4/lib/common/compiler.h +0 -159
  122. data/ext/zstdlib/zstd-1.4.4/lib/common/debug.c +0 -44
  123. data/ext/zstdlib/zstd-1.4.4/lib/common/entropy_common.c +0 -236
  124. data/ext/zstdlib/zstd-1.4.4/lib/common/error_private.h +0 -76
  125. data/ext/zstdlib/zstd-1.4.4/lib/common/xxhash.c +0 -882
  126. data/ext/zstdlib/zstd-1.4.4/lib/common/xxhash.h +0 -305
  127. data/ext/zstdlib/zstd-1.4.4/lib/common/zstd_internal.h +0 -350
  128. data/ext/zstdlib/zstd-1.4.4/lib/compress/huf_compress.c +0 -798
  129. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_compress.c +0 -4103
  130. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_lazy.c +0 -1115
  131. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_lazy.h +0 -67
  132. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstdmt_compress.h +0 -192
  133. data/ext/zstdlib/zstd-1.4.4/lib/decompress/huf_decompress.c +0 -1234
  134. data/ext/zstdlib/zstd-1.4.4/lib/decompress/zstd_decompress_block.c +0 -1323
  135. data/ext/zstdlib/zstd.mk +0 -14
  136. data/lib/2.2/zstdlib.so +0 -0
  137. data/lib/2.3/zstdlib.so +0 -0
  138. data/lib/2.4/zstdlib.so +0 -0
  139. data/lib/2.5/zstdlib.so +0 -0
  140. data/lib/2.6/zstdlib.so +0 -0
  141. data/lib/2.7/zstdlib.so +0 -0
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -20,11 +20,10 @@
20
20
 
21
21
 
22
22
  /* ====== Dependencies ====== */
23
- #include <string.h> /* memcpy, memset */
24
- #include <limits.h> /* INT_MAX, UINT_MAX */
25
- #include "mem.h" /* MEM_STATIC */
26
- #include "pool.h" /* threadpool */
27
- #include "threading.h" /* mutex */
23
+ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset, INT_MAX, UINT_MAX */
24
+ #include "../common/mem.h" /* MEM_STATIC */
25
+ #include "../common/pool.h" /* threadpool */
26
+ #include "../common/threading.h" /* mutex */
28
27
  #include "zstd_compress_internal.h" /* MIN, ERROR, ZSTD_*, ZSTD_highbit32 */
29
28
  #include "zstd_ldm.h"
30
29
  #include "zstdmt_compress.h"
@@ -103,14 +102,13 @@ typedef struct ZSTDMT_bufferPool_s {
103
102
  buffer_t bTable[1]; /* variable size */
104
103
  } ZSTDMT_bufferPool;
105
104
 
106
- static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned nbWorkers, ZSTD_customMem cMem)
105
+ static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned maxNbBuffers, ZSTD_customMem cMem)
107
106
  {
108
- unsigned const maxNbBuffers = 2*nbWorkers + 3;
109
- ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)ZSTD_calloc(
107
+ ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)ZSTD_customCalloc(
110
108
  sizeof(ZSTDMT_bufferPool) + (maxNbBuffers-1) * sizeof(buffer_t), cMem);
111
109
  if (bufPool==NULL) return NULL;
112
110
  if (ZSTD_pthread_mutex_init(&bufPool->poolMutex, NULL)) {
113
- ZSTD_free(bufPool, cMem);
111
+ ZSTD_customFree(bufPool, cMem);
114
112
  return NULL;
115
113
  }
116
114
  bufPool->bufferSize = 64 KB;
@@ -127,10 +125,10 @@ static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool* bufPool)
127
125
  if (!bufPool) return; /* compatibility with free on NULL */
128
126
  for (u=0; u<bufPool->totalBuffers; u++) {
129
127
  DEBUGLOG(4, "free buffer %2u (address:%08X)", u, (U32)(size_t)bufPool->bTable[u].start);
130
- ZSTD_free(bufPool->bTable[u].start, bufPool->cMem);
128
+ ZSTD_customFree(bufPool->bTable[u].start, bufPool->cMem);
131
129
  }
132
130
  ZSTD_pthread_mutex_destroy(&bufPool->poolMutex);
133
- ZSTD_free(bufPool, bufPool->cMem);
131
+ ZSTD_customFree(bufPool, bufPool->cMem);
134
132
  }
135
133
 
136
134
  /* only works at initialization, not during compression */
@@ -161,9 +159,8 @@ static void ZSTDMT_setBufferSize(ZSTDMT_bufferPool* const bufPool, size_t const
161
159
  }
162
160
 
163
161
 
164
- static ZSTDMT_bufferPool* ZSTDMT_expandBufferPool(ZSTDMT_bufferPool* srcBufPool, U32 nbWorkers)
162
+ static ZSTDMT_bufferPool* ZSTDMT_expandBufferPool(ZSTDMT_bufferPool* srcBufPool, unsigned maxNbBuffers)
165
163
  {
166
- unsigned const maxNbBuffers = 2*nbWorkers + 3;
167
164
  if (srcBufPool==NULL) return NULL;
168
165
  if (srcBufPool->totalBuffers >= maxNbBuffers) /* good enough */
169
166
  return srcBufPool;
@@ -172,7 +169,7 @@ static ZSTDMT_bufferPool* ZSTDMT_expandBufferPool(ZSTDMT_bufferPool* srcBufPool,
172
169
  size_t const bSize = srcBufPool->bufferSize; /* forward parameters */
173
170
  ZSTDMT_bufferPool* newBufPool;
174
171
  ZSTDMT_freeBufferPool(srcBufPool);
175
- newBufPool = ZSTDMT_createBufferPool(nbWorkers, cMem);
172
+ newBufPool = ZSTDMT_createBufferPool(maxNbBuffers, cMem);
176
173
  if (newBufPool==NULL) return newBufPool;
177
174
  ZSTDMT_setBufferSize(newBufPool, bSize);
178
175
  return newBufPool;
@@ -201,13 +198,13 @@ static buffer_t ZSTDMT_getBuffer(ZSTDMT_bufferPool* bufPool)
201
198
  }
202
199
  /* size conditions not respected : scratch this buffer, create new one */
203
200
  DEBUGLOG(5, "ZSTDMT_getBuffer: existing buffer does not meet size conditions => freeing");
204
- ZSTD_free(buf.start, bufPool->cMem);
201
+ ZSTD_customFree(buf.start, bufPool->cMem);
205
202
  }
206
203
  ZSTD_pthread_mutex_unlock(&bufPool->poolMutex);
207
204
  /* create new buffer */
208
205
  DEBUGLOG(5, "ZSTDMT_getBuffer: create a new buffer");
209
206
  { buffer_t buffer;
210
- void* const start = ZSTD_malloc(bSize, bufPool->cMem);
207
+ void* const start = ZSTD_customMalloc(bSize, bufPool->cMem);
211
208
  buffer.start = start; /* note : start can be NULL if malloc fails ! */
212
209
  buffer.capacity = (start==NULL) ? 0 : bSize;
213
210
  if (start==NULL) {
@@ -229,13 +226,13 @@ static buffer_t ZSTDMT_resizeBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buffer)
229
226
  {
230
227
  size_t const bSize = bufPool->bufferSize;
231
228
  if (buffer.capacity < bSize) {
232
- void* const start = ZSTD_malloc(bSize, bufPool->cMem);
229
+ void* const start = ZSTD_customMalloc(bSize, bufPool->cMem);
233
230
  buffer_t newBuffer;
234
231
  newBuffer.start = start;
235
232
  newBuffer.capacity = start == NULL ? 0 : bSize;
236
233
  if (start != NULL) {
237
234
  assert(newBuffer.capacity >= buffer.capacity);
238
- memcpy(newBuffer.start, buffer.start, buffer.capacity);
235
+ ZSTD_memcpy(newBuffer.start, buffer.start, buffer.capacity);
239
236
  DEBUGLOG(5, "ZSTDMT_resizeBuffer: created buffer of size %u", (U32)bSize);
240
237
  return newBuffer;
241
238
  }
@@ -261,13 +258,21 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf)
261
258
  ZSTD_pthread_mutex_unlock(&bufPool->poolMutex);
262
259
  /* Reached bufferPool capacity (should not happen) */
263
260
  DEBUGLOG(5, "ZSTDMT_releaseBuffer: pool capacity reached => freeing ");
264
- ZSTD_free(buf.start, bufPool->cMem);
261
+ ZSTD_customFree(buf.start, bufPool->cMem);
265
262
  }
266
263
 
264
+ /* We need 2 output buffers per worker since each dstBuff must be flushed after it is released.
265
+ * The 3 additional buffers are as follows:
266
+ * 1 buffer for input loading
267
+ * 1 buffer for "next input" when submitting current one
268
+ * 1 buffer stuck in queue */
269
+ #define BUF_POOL_MAX_NB_BUFFERS(nbWorkers) 2*nbWorkers + 3
267
270
 
268
- /* ===== Seq Pool Wrapper ====== */
271
+ /* After a worker releases its rawSeqStore, it is immediately ready for reuse.
272
+ * So we only need one seq buffer per worker. */
273
+ #define SEQ_POOL_MAX_NB_BUFFERS(nbWorkers) nbWorkers
269
274
 
270
- static rawSeqStore_t kNullRawSeqStore = {NULL, 0, 0, 0};
275
+ /* ===== Seq Pool Wrapper ====== */
271
276
 
272
277
  typedef ZSTDMT_bufferPool ZSTDMT_seqPool;
273
278
 
@@ -278,7 +283,7 @@ static size_t ZSTDMT_sizeof_seqPool(ZSTDMT_seqPool* seqPool)
278
283
 
279
284
  static rawSeqStore_t bufferToSeq(buffer_t buffer)
280
285
  {
281
- rawSeqStore_t seq = {NULL, 0, 0, 0};
286
+ rawSeqStore_t seq = kNullRawSeqStore;
282
287
  seq.seq = (rawSeq*)buffer.start;
283
288
  seq.capacity = buffer.capacity / sizeof(rawSeq);
284
289
  return seq;
@@ -319,7 +324,7 @@ static void ZSTDMT_setNbSeq(ZSTDMT_seqPool* const seqPool, size_t const nbSeq)
319
324
 
320
325
  static ZSTDMT_seqPool* ZSTDMT_createSeqPool(unsigned nbWorkers, ZSTD_customMem cMem)
321
326
  {
322
- ZSTDMT_seqPool* const seqPool = ZSTDMT_createBufferPool(nbWorkers, cMem);
327
+ ZSTDMT_seqPool* const seqPool = ZSTDMT_createBufferPool(SEQ_POOL_MAX_NB_BUFFERS(nbWorkers), cMem);
323
328
  if (seqPool == NULL) return NULL;
324
329
  ZSTDMT_setNbSeq(seqPool, 0);
325
330
  return seqPool;
@@ -332,7 +337,7 @@ static void ZSTDMT_freeSeqPool(ZSTDMT_seqPool* seqPool)
332
337
 
333
338
  static ZSTDMT_seqPool* ZSTDMT_expandSeqPool(ZSTDMT_seqPool* pool, U32 nbWorkers)
334
339
  {
335
- return ZSTDMT_expandBufferPool(pool, nbWorkers);
340
+ return ZSTDMT_expandBufferPool(pool, SEQ_POOL_MAX_NB_BUFFERS(nbWorkers));
336
341
  }
337
342
 
338
343
 
@@ -354,7 +359,7 @@ static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
354
359
  for (cid=0; cid<pool->totalCCtx; cid++)
355
360
  ZSTD_freeCCtx(pool->cctx[cid]); /* note : compatible with free on NULL */
356
361
  ZSTD_pthread_mutex_destroy(&pool->poolMutex);
357
- ZSTD_free(pool, pool->cMem);
362
+ ZSTD_customFree(pool, pool->cMem);
358
363
  }
359
364
 
360
365
  /* ZSTDMT_createCCtxPool() :
@@ -362,12 +367,12 @@ static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
362
367
  static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(int nbWorkers,
363
368
  ZSTD_customMem cMem)
364
369
  {
365
- ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) ZSTD_calloc(
370
+ ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) ZSTD_customCalloc(
366
371
  sizeof(ZSTDMT_CCtxPool) + (nbWorkers-1)*sizeof(ZSTD_CCtx*), cMem);
367
372
  assert(nbWorkers > 0);
368
373
  if (!cctxPool) return NULL;
369
374
  if (ZSTD_pthread_mutex_init(&cctxPool->poolMutex, NULL)) {
370
- ZSTD_free(cctxPool, cMem);
375
+ ZSTD_customFree(cctxPool, cMem);
371
376
  return NULL;
372
377
  }
373
378
  cctxPool->cMem = cMem;
@@ -461,52 +466,72 @@ typedef struct {
461
466
  ZSTD_window_t ldmWindow; /* A thread-safe copy of ldmState.window */
462
467
  } serialState_t;
463
468
 
464
- static int ZSTDMT_serialState_reset(serialState_t* serialState, ZSTDMT_seqPool* seqPool, ZSTD_CCtx_params params, size_t jobSize)
469
+ static int
470
+ ZSTDMT_serialState_reset(serialState_t* serialState,
471
+ ZSTDMT_seqPool* seqPool,
472
+ ZSTD_CCtx_params params,
473
+ size_t jobSize,
474
+ const void* dict, size_t const dictSize,
475
+ ZSTD_dictContentType_e dictContentType)
465
476
  {
466
477
  /* Adjust parameters */
467
- if (params.ldmParams.enableLdm) {
478
+ if (params.ldmParams.enableLdm == ZSTD_ps_enable) {
468
479
  DEBUGLOG(4, "LDM window size = %u KB", (1U << params.cParams.windowLog) >> 10);
469
480
  ZSTD_ldm_adjustParameters(&params.ldmParams, &params.cParams);
470
481
  assert(params.ldmParams.hashLog >= params.ldmParams.bucketSizeLog);
471
482
  assert(params.ldmParams.hashRateLog < 32);
472
- serialState->ldmState.hashPower =
473
- ZSTD_rollingHash_primePower(params.ldmParams.minMatchLength);
474
483
  } else {
475
- memset(&params.ldmParams, 0, sizeof(params.ldmParams));
484
+ ZSTD_memset(&params.ldmParams, 0, sizeof(params.ldmParams));
476
485
  }
477
486
  serialState->nextJobID = 0;
478
487
  if (params.fParams.checksumFlag)
479
488
  XXH64_reset(&serialState->xxhState, 0);
480
- if (params.ldmParams.enableLdm) {
489
+ if (params.ldmParams.enableLdm == ZSTD_ps_enable) {
481
490
  ZSTD_customMem cMem = params.customMem;
482
491
  unsigned const hashLog = params.ldmParams.hashLog;
483
492
  size_t const hashSize = ((size_t)1 << hashLog) * sizeof(ldmEntry_t);
484
493
  unsigned const bucketLog =
485
494
  params.ldmParams.hashLog - params.ldmParams.bucketSizeLog;
486
- size_t const bucketSize = (size_t)1 << bucketLog;
487
495
  unsigned const prevBucketLog =
488
496
  serialState->params.ldmParams.hashLog -
489
497
  serialState->params.ldmParams.bucketSizeLog;
498
+ size_t const numBuckets = (size_t)1 << bucketLog;
490
499
  /* Size the seq pool tables */
491
500
  ZSTDMT_setNbSeq(seqPool, ZSTD_ldm_getMaxNbSeq(params.ldmParams, jobSize));
492
501
  /* Reset the window */
493
- ZSTD_window_clear(&serialState->ldmState.window);
494
- serialState->ldmWindow = serialState->ldmState.window;
502
+ ZSTD_window_init(&serialState->ldmState.window);
495
503
  /* Resize tables and output space if necessary. */
496
504
  if (serialState->ldmState.hashTable == NULL || serialState->params.ldmParams.hashLog < hashLog) {
497
- ZSTD_free(serialState->ldmState.hashTable, cMem);
498
- serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_malloc(hashSize, cMem);
505
+ ZSTD_customFree(serialState->ldmState.hashTable, cMem);
506
+ serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_customMalloc(hashSize, cMem);
499
507
  }
500
508
  if (serialState->ldmState.bucketOffsets == NULL || prevBucketLog < bucketLog) {
501
- ZSTD_free(serialState->ldmState.bucketOffsets, cMem);
502
- serialState->ldmState.bucketOffsets = (BYTE*)ZSTD_malloc(bucketSize, cMem);
509
+ ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem);
510
+ serialState->ldmState.bucketOffsets = (BYTE*)ZSTD_customMalloc(numBuckets, cMem);
503
511
  }
504
512
  if (!serialState->ldmState.hashTable || !serialState->ldmState.bucketOffsets)
505
513
  return 1;
506
514
  /* Zero the tables */
507
- memset(serialState->ldmState.hashTable, 0, hashSize);
508
- memset(serialState->ldmState.bucketOffsets, 0, bucketSize);
515
+ ZSTD_memset(serialState->ldmState.hashTable, 0, hashSize);
516
+ ZSTD_memset(serialState->ldmState.bucketOffsets, 0, numBuckets);
517
+
518
+ /* Update window state and fill hash table with dict */
519
+ serialState->ldmState.loadedDictEnd = 0;
520
+ if (dictSize > 0) {
521
+ if (dictContentType == ZSTD_dct_rawContent) {
522
+ BYTE const* const dictEnd = (const BYTE*)dict + dictSize;
523
+ ZSTD_window_update(&serialState->ldmState.window, dict, dictSize, /* forceNonContiguous */ 0);
524
+ ZSTD_ldm_fillHashTable(&serialState->ldmState, (const BYTE*)dict, dictEnd, &params.ldmParams);
525
+ serialState->ldmState.loadedDictEnd = params.forceWindow ? 0 : (U32)(dictEnd - serialState->ldmState.window.base);
526
+ } else {
527
+ /* don't even load anything */
528
+ }
529
+ }
530
+
531
+ /* Initialize serialState's copy of ldmWindow. */
532
+ serialState->ldmWindow = serialState->ldmState.window;
509
533
  }
534
+
510
535
  serialState->params = params;
511
536
  serialState->params.jobSize = (U32)jobSize;
512
537
  return 0;
@@ -515,7 +540,7 @@ static int ZSTDMT_serialState_reset(serialState_t* serialState, ZSTDMT_seqPool*
515
540
  static int ZSTDMT_serialState_init(serialState_t* serialState)
516
541
  {
517
542
  int initError = 0;
518
- memset(serialState, 0, sizeof(*serialState));
543
+ ZSTD_memset(serialState, 0, sizeof(*serialState));
519
544
  initError |= ZSTD_pthread_mutex_init(&serialState->mutex, NULL);
520
545
  initError |= ZSTD_pthread_cond_init(&serialState->cond, NULL);
521
546
  initError |= ZSTD_pthread_mutex_init(&serialState->ldmWindowMutex, NULL);
@@ -530,8 +555,8 @@ static void ZSTDMT_serialState_free(serialState_t* serialState)
530
555
  ZSTD_pthread_cond_destroy(&serialState->cond);
531
556
  ZSTD_pthread_mutex_destroy(&serialState->ldmWindowMutex);
532
557
  ZSTD_pthread_cond_destroy(&serialState->ldmWindowCond);
533
- ZSTD_free(serialState->ldmState.hashTable, cMem);
534
- ZSTD_free(serialState->ldmState.bucketOffsets, cMem);
558
+ ZSTD_customFree(serialState->ldmState.hashTable, cMem);
559
+ ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem);
535
560
  }
536
561
 
537
562
  static void ZSTDMT_serialState_update(serialState_t* serialState,
@@ -547,12 +572,12 @@ static void ZSTDMT_serialState_update(serialState_t* serialState,
547
572
  /* A future job may error and skip our job */
548
573
  if (serialState->nextJobID == jobID) {
549
574
  /* It is now our turn, do any processing necessary */
550
- if (serialState->params.ldmParams.enableLdm) {
575
+ if (serialState->params.ldmParams.enableLdm == ZSTD_ps_enable) {
551
576
  size_t error;
552
577
  assert(seqStore.seq != NULL && seqStore.pos == 0 &&
553
578
  seqStore.size == 0 && seqStore.capacity > 0);
554
579
  assert(src.size <= serialState->params.jobSize);
555
- ZSTD_window_update(&serialState->ldmState.window, src.start, src.size);
580
+ ZSTD_window_update(&serialState->ldmState.window, src.start, src.size, /* forceNonContiguous */ 0);
556
581
  error = ZSTD_ldm_generateSequences(
557
582
  &serialState->ldmState, &seqStore,
558
583
  &serialState->params.ldmParams, src.start, src.size);
@@ -577,7 +602,7 @@ static void ZSTDMT_serialState_update(serialState_t* serialState,
577
602
  if (seqStore.size > 0) {
578
603
  size_t const err = ZSTD_referenceExternalSequences(
579
604
  jobCCtx, seqStore.seq, seqStore.size);
580
- assert(serialState->params.ldmParams.enableLdm);
605
+ assert(serialState->params.ldmParams.enableLdm == ZSTD_ps_enable);
581
606
  assert(!ZSTD_isError(err));
582
607
  (void)err;
583
608
  }
@@ -655,7 +680,7 @@ static void ZSTDMT_compressionJob(void* jobDescription)
655
680
  if (dstBuff.start==NULL) JOB_ERROR(ERROR(memory_allocation));
656
681
  job->dstBuff = dstBuff; /* this value can be read in ZSTDMT_flush, when it copies the whole job */
657
682
  }
658
- if (jobParams.ldmParams.enableLdm && rawSeqStore.seq == NULL)
683
+ if (jobParams.ldmParams.enableLdm == ZSTD_ps_enable && rawSeqStore.seq == NULL)
659
684
  JOB_ERROR(ERROR(memory_allocation));
660
685
 
661
686
  /* Don't compute the checksum for chunks, since we compute it externally,
@@ -663,7 +688,9 @@ static void ZSTDMT_compressionJob(void* jobDescription)
663
688
  */
664
689
  if (job->jobID != 0) jobParams.fParams.checksumFlag = 0;
665
690
  /* Don't run LDM for the chunks, since we handle it externally */
666
- jobParams.ldmParams.enableLdm = 0;
691
+ jobParams.ldmParams.enableLdm = ZSTD_ps_disable;
692
+ /* Correct nbWorkers to 0. */
693
+ jobParams.nbWorkers = 0;
667
694
 
668
695
 
669
696
  /* init */
@@ -676,6 +703,10 @@ static void ZSTDMT_compressionJob(void* jobDescription)
676
703
  { size_t const forceWindowError = ZSTD_CCtxParams_setParameter(&jobParams, ZSTD_c_forceMaxWindow, !job->firstJob);
677
704
  if (ZSTD_isError(forceWindowError)) JOB_ERROR(forceWindowError);
678
705
  }
706
+ if (!job->firstJob) {
707
+ size_t const err = ZSTD_CCtxParams_setParameter(&jobParams, ZSTD_c_deterministicRefPrefix, 0);
708
+ if (ZSTD_isError(err)) JOB_ERROR(err);
709
+ }
679
710
  { size_t const initError = ZSTD_compressBegin_advanced_internal(cctx,
680
711
  job->prefix.start, job->prefix.size, ZSTD_dct_rawContent, /* load dictionary in "content-only" mode (no header analysis) */
681
712
  ZSTD_dtlm_fast,
@@ -731,6 +762,13 @@ static void ZSTDMT_compressionJob(void* jobDescription)
731
762
  if (ZSTD_isError(cSize)) JOB_ERROR(cSize);
732
763
  lastCBlockSize = cSize;
733
764
  } }
765
+ if (!job->firstJob) {
766
+ /* Double check that we don't have an ext-dict, because then our
767
+ * repcode invalidation doesn't work.
768
+ */
769
+ assert(!ZSTD_window_hasExtDict(cctx->blockState.matchState.window));
770
+ }
771
+ ZSTD_CCtx_trace(cctx, 0);
734
772
 
735
773
  _endJob:
736
774
  ZSTDMT_serialState_ensureFinished(job->serial, job->jobID, job->cSize);
@@ -777,6 +815,15 @@ typedef struct {
777
815
  static const roundBuff_t kNullRoundBuff = {NULL, 0, 0};
778
816
 
779
817
  #define RSYNC_LENGTH 32
818
+ /* Don't create chunks smaller than the zstd block size.
819
+ * This stops us from regressing compression ratio too much,
820
+ * and ensures our output fits in ZSTD_compressBound().
821
+ *
822
+ * If this is shrunk < ZSTD_BLOCKSIZELOG_MIN then
823
+ * ZSTD_COMPRESSBOUND() will need to be updated.
824
+ */
825
+ #define RSYNC_MIN_BLOCK_LOG ZSTD_BLOCKSIZELOG_MAX
826
+ #define RSYNC_MIN_BLOCK_SIZE (1<<RSYNC_MIN_BLOCK_LOG)
780
827
 
781
828
  typedef struct {
782
829
  U64 hash;
@@ -798,7 +845,6 @@ struct ZSTDMT_CCtx_s {
798
845
  roundBuff_t roundBuff;
799
846
  serialState_t serial;
800
847
  rsyncState_t rsync;
801
- unsigned singleBlockingThread;
802
848
  unsigned jobIDMask;
803
849
  unsigned doneJobID;
804
850
  unsigned nextJobID;
@@ -810,6 +856,7 @@ struct ZSTDMT_CCtx_s {
810
856
  ZSTD_customMem cMem;
811
857
  ZSTD_CDict* cdictLocal;
812
858
  const ZSTD_CDict* cdict;
859
+ unsigned providedFactory: 1;
813
860
  };
814
861
 
815
862
  static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, U32 nbJobs, ZSTD_customMem cMem)
@@ -820,7 +867,7 @@ static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, U32 nbJobs, ZS
820
867
  ZSTD_pthread_mutex_destroy(&jobTable[jobNb].job_mutex);
821
868
  ZSTD_pthread_cond_destroy(&jobTable[jobNb].job_cond);
822
869
  }
823
- ZSTD_free(jobTable, cMem);
870
+ ZSTD_customFree(jobTable, cMem);
824
871
  }
825
872
 
826
873
  /* ZSTDMT_allocJobsTable()
@@ -832,7 +879,7 @@ static ZSTDMT_jobDescription* ZSTDMT_createJobsTable(U32* nbJobsPtr, ZSTD_custom
832
879
  U32 const nbJobs = 1 << nbJobsLog2;
833
880
  U32 jobNb;
834
881
  ZSTDMT_jobDescription* const jobTable = (ZSTDMT_jobDescription*)
835
- ZSTD_calloc(nbJobs * sizeof(ZSTDMT_jobDescription), cMem);
882
+ ZSTD_customCalloc(nbJobs * sizeof(ZSTDMT_jobDescription), cMem);
836
883
  int initError = 0;
837
884
  if (jobTable==NULL) return NULL;
838
885
  *nbJobsPtr = nbJobs;
@@ -863,12 +910,12 @@ static size_t ZSTDMT_expandJobsTable (ZSTDMT_CCtx* mtctx, U32 nbWorkers) {
863
910
 
864
911
  /* ZSTDMT_CCtxParam_setNbWorkers():
865
912
  * Internal use only */
866
- size_t ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params* params, unsigned nbWorkers)
913
+ static size_t ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params* params, unsigned nbWorkers)
867
914
  {
868
915
  return ZSTD_CCtxParams_setParameter(params, ZSTD_c_nbWorkers, (int)nbWorkers);
869
916
  }
870
917
 
871
- MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers, ZSTD_customMem cMem)
918
+ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers, ZSTD_customMem cMem, ZSTD_threadPool* pool)
872
919
  {
873
920
  ZSTDMT_CCtx* mtctx;
874
921
  U32 nbJobs = nbWorkers + 2;
@@ -881,16 +928,23 @@ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers,
881
928
  /* invalid custom allocator */
882
929
  return NULL;
883
930
 
884
- mtctx = (ZSTDMT_CCtx*) ZSTD_calloc(sizeof(ZSTDMT_CCtx), cMem);
931
+ mtctx = (ZSTDMT_CCtx*) ZSTD_customCalloc(sizeof(ZSTDMT_CCtx), cMem);
885
932
  if (!mtctx) return NULL;
886
933
  ZSTDMT_CCtxParam_setNbWorkers(&mtctx->params, nbWorkers);
887
934
  mtctx->cMem = cMem;
888
935
  mtctx->allJobsCompleted = 1;
889
- mtctx->factory = POOL_create_advanced(nbWorkers, 0, cMem);
936
+ if (pool != NULL) {
937
+ mtctx->factory = pool;
938
+ mtctx->providedFactory = 1;
939
+ }
940
+ else {
941
+ mtctx->factory = POOL_create_advanced(nbWorkers, 0, cMem);
942
+ mtctx->providedFactory = 0;
943
+ }
890
944
  mtctx->jobs = ZSTDMT_createJobsTable(&nbJobs, cMem);
891
945
  assert(nbJobs > 0); assert((nbJobs & (nbJobs - 1)) == 0); /* ensure nbJobs is a power of 2 */
892
946
  mtctx->jobIDMask = nbJobs - 1;
893
- mtctx->bufPool = ZSTDMT_createBufferPool(nbWorkers, cMem);
947
+ mtctx->bufPool = ZSTDMT_createBufferPool(BUF_POOL_MAX_NB_BUFFERS(nbWorkers), cMem);
894
948
  mtctx->cctxPool = ZSTDMT_createCCtxPool(nbWorkers, cMem);
895
949
  mtctx->seqPool = ZSTDMT_createSeqPool(nbWorkers, cMem);
896
950
  initError = ZSTDMT_serialState_init(&mtctx->serial);
@@ -903,22 +957,18 @@ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers,
903
957
  return mtctx;
904
958
  }
905
959
 
906
- ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers, ZSTD_customMem cMem)
960
+ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers, ZSTD_customMem cMem, ZSTD_threadPool* pool)
907
961
  {
908
962
  #ifdef ZSTD_MULTITHREAD
909
- return ZSTDMT_createCCtx_advanced_internal(nbWorkers, cMem);
963
+ return ZSTDMT_createCCtx_advanced_internal(nbWorkers, cMem, pool);
910
964
  #else
911
965
  (void)nbWorkers;
912
966
  (void)cMem;
967
+ (void)pool;
913
968
  return NULL;
914
969
  #endif
915
970
  }
916
971
 
917
- ZSTDMT_CCtx* ZSTDMT_createCCtx(unsigned nbWorkers)
918
- {
919
- return ZSTDMT_createCCtx_advanced(nbWorkers, ZSTD_defaultCMem);
920
- }
921
-
922
972
 
923
973
  /* ZSTDMT_releaseAllJobResources() :
924
974
  * note : ensure all workers are killed first ! */
@@ -935,7 +985,7 @@ static void ZSTDMT_releaseAllJobResources(ZSTDMT_CCtx* mtctx)
935
985
  ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
936
986
 
937
987
  /* Clear the job description, but keep the mutex/cond */
938
- memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
988
+ ZSTD_memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
939
989
  mtctx->jobs[jobID].job_mutex = mutex;
940
990
  mtctx->jobs[jobID].job_cond = cond;
941
991
  }
@@ -962,7 +1012,8 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* mtctx)
962
1012
  size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
963
1013
  {
964
1014
  if (mtctx==NULL) return 0; /* compatible with free on NULL */
965
- POOL_free(mtctx->factory); /* stop and free worker threads */
1015
+ if (!mtctx->providedFactory)
1016
+ POOL_free(mtctx->factory); /* stop and free worker threads */
966
1017
  ZSTDMT_releaseAllJobResources(mtctx); /* release job resources into pools first */
967
1018
  ZSTDMT_freeJobsTable(mtctx->jobs, mtctx->jobIDMask+1, mtctx->cMem);
968
1019
  ZSTDMT_freeBufferPool(mtctx->bufPool);
@@ -971,8 +1022,8 @@ size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
971
1022
  ZSTDMT_serialState_free(&mtctx->serial);
972
1023
  ZSTD_freeCDict(mtctx->cdictLocal);
973
1024
  if (mtctx->roundBuff.buffer)
974
- ZSTD_free(mtctx->roundBuff.buffer, mtctx->cMem);
975
- ZSTD_free(mtctx, mtctx->cMem);
1025
+ ZSTD_customFree(mtctx->roundBuff.buffer, mtctx->cMem);
1026
+ ZSTD_customFree(mtctx, mtctx->cMem);
976
1027
  return 0;
977
1028
  }
978
1029
 
@@ -989,73 +1040,14 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx)
989
1040
  + mtctx->roundBuff.capacity;
990
1041
  }
991
1042
 
992
- /* Internal only */
993
- size_t
994
- ZSTDMT_CCtxParam_setMTCtxParameter(ZSTD_CCtx_params* params,
995
- ZSTDMT_parameter parameter,
996
- int value)
997
- {
998
- DEBUGLOG(4, "ZSTDMT_CCtxParam_setMTCtxParameter");
999
- switch(parameter)
1000
- {
1001
- case ZSTDMT_p_jobSize :
1002
- DEBUGLOG(4, "ZSTDMT_CCtxParam_setMTCtxParameter : set jobSize to %i", value);
1003
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_jobSize, value);
1004
- case ZSTDMT_p_overlapLog :
1005
- DEBUGLOG(4, "ZSTDMT_p_overlapLog : %i", value);
1006
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_overlapLog, value);
1007
- case ZSTDMT_p_rsyncable :
1008
- DEBUGLOG(4, "ZSTD_p_rsyncable : %i", value);
1009
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_rsyncable, value);
1010
- default :
1011
- return ERROR(parameter_unsupported);
1012
- }
1013
- }
1014
-
1015
- size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, int value)
1016
- {
1017
- DEBUGLOG(4, "ZSTDMT_setMTCtxParameter");
1018
- return ZSTDMT_CCtxParam_setMTCtxParameter(&mtctx->params, parameter, value);
1019
- }
1020
-
1021
- size_t ZSTDMT_getMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, int* value)
1022
- {
1023
- switch (parameter) {
1024
- case ZSTDMT_p_jobSize:
1025
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_jobSize, value);
1026
- case ZSTDMT_p_overlapLog:
1027
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_overlapLog, value);
1028
- case ZSTDMT_p_rsyncable:
1029
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_rsyncable, value);
1030
- default:
1031
- return ERROR(parameter_unsupported);
1032
- }
1033
- }
1034
-
1035
- /* Sets parameters relevant to the compression job,
1036
- * initializing others to default values. */
1037
- static ZSTD_CCtx_params ZSTDMT_initJobCCtxParams(const ZSTD_CCtx_params* params)
1038
- {
1039
- ZSTD_CCtx_params jobParams = *params;
1040
- /* Clear parameters related to multithreading */
1041
- jobParams.forceWindow = 0;
1042
- jobParams.nbWorkers = 0;
1043
- jobParams.jobSize = 0;
1044
- jobParams.overlapLog = 0;
1045
- jobParams.rsyncable = 0;
1046
- memset(&jobParams.ldmParams, 0, sizeof(ldmParams_t));
1047
- memset(&jobParams.customMem, 0, sizeof(ZSTD_customMem));
1048
- return jobParams;
1049
- }
1050
-
1051
1043
 
1052
1044
  /* ZSTDMT_resize() :
1053
1045
  * @return : error code if fails, 0 on success */
1054
1046
  static size_t ZSTDMT_resize(ZSTDMT_CCtx* mtctx, unsigned nbWorkers)
1055
1047
  {
1056
1048
  if (POOL_resize(mtctx->factory, nbWorkers)) return ERROR(memory_allocation);
1057
- FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbWorkers) );
1058
- mtctx->bufPool = ZSTDMT_expandBufferPool(mtctx->bufPool, nbWorkers);
1049
+ FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbWorkers) , "");
1050
+ mtctx->bufPool = ZSTDMT_expandBufferPool(mtctx->bufPool, BUF_POOL_MAX_NB_BUFFERS(nbWorkers));
1059
1051
  if (mtctx->bufPool == NULL) return ERROR(memory_allocation);
1060
1052
  mtctx->cctxPool = ZSTDMT_expandCCtxPool(mtctx->cctxPool, nbWorkers);
1061
1053
  if (mtctx->cctxPool == NULL) return ERROR(memory_allocation);
@@ -1076,7 +1068,7 @@ void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx* mtctx, const ZSTD_CCtx_p
1076
1068
  DEBUGLOG(5, "ZSTDMT_updateCParams_whileCompressing (level:%i)",
1077
1069
  compressionLevel);
1078
1070
  mtctx->params.compressionLevel = compressionLevel;
1079
- { ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(cctxParams, 0, 0);
1071
+ { ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, 0, ZSTD_cpm_noAttachDict);
1080
1072
  cParams.windowLog = saved_wlog;
1081
1073
  mtctx->params.cParams = cParams;
1082
1074
  }
@@ -1160,11 +1152,11 @@ size_t ZSTDMT_toFlushNow(ZSTDMT_CCtx* mtctx)
1160
1152
  static unsigned ZSTDMT_computeTargetJobLog(const ZSTD_CCtx_params* params)
1161
1153
  {
1162
1154
  unsigned jobLog;
1163
- if (params->ldmParams.enableLdm) {
1155
+ if (params->ldmParams.enableLdm == ZSTD_ps_enable) {
1164
1156
  /* In Long Range Mode, the windowLog is typically oversized.
1165
1157
  * In which case, it's preferable to determine the jobSize
1166
- * based on chainLog instead. */
1167
- jobLog = MAX(21, params->cParams.chainLog + 4);
1158
+ * based on cycleLog instead. */
1159
+ jobLog = MAX(21, ZSTD_cycleLog(params->cParams.chainLog, params->cParams.strategy) + 3);
1168
1160
  } else {
1169
1161
  jobLog = MAX(20, params->cParams.windowLog + 2);
1170
1162
  }
@@ -1204,7 +1196,7 @@ static size_t ZSTDMT_computeOverlapSize(const ZSTD_CCtx_params* params)
1204
1196
  int const overlapRLog = 9 - ZSTDMT_overlapLog(params->overlapLog, params->cParams.strategy);
1205
1197
  int ovLog = (overlapRLog >= 8) ? 0 : (params->cParams.windowLog - overlapRLog);
1206
1198
  assert(0 <= overlapRLog && overlapRLog <= 8);
1207
- if (params->ldmParams.enableLdm) {
1199
+ if (params->ldmParams.enableLdm == ZSTD_ps_enable) {
1208
1200
  /* In Long Range Mode, the windowLog is typically oversized.
1209
1201
  * In which case, it's preferable to determine the jobSize
1210
1202
  * based on chainLog instead.
@@ -1218,172 +1210,6 @@ static size_t ZSTDMT_computeOverlapSize(const ZSTD_CCtx_params* params)
1218
1210
  return (ovLog==0) ? 0 : (size_t)1 << ovLog;
1219
1211
  }
1220
1212
 
1221
- static unsigned
1222
- ZSTDMT_computeNbJobs(const ZSTD_CCtx_params* params, size_t srcSize, unsigned nbWorkers)
1223
- {
1224
- assert(nbWorkers>0);
1225
- { size_t const jobSizeTarget = (size_t)1 << ZSTDMT_computeTargetJobLog(params);
1226
- size_t const jobMaxSize = jobSizeTarget << 2;
1227
- size_t const passSizeMax = jobMaxSize * nbWorkers;
1228
- unsigned const multiplier = (unsigned)(srcSize / passSizeMax) + 1;
1229
- unsigned const nbJobsLarge = multiplier * nbWorkers;
1230
- unsigned const nbJobsMax = (unsigned)(srcSize / jobSizeTarget) + 1;
1231
- unsigned const nbJobsSmall = MIN(nbJobsMax, nbWorkers);
1232
- return (multiplier>1) ? nbJobsLarge : nbJobsSmall;
1233
- } }
1234
-
1235
- /* ZSTDMT_compress_advanced_internal() :
1236
- * This is a blocking function : it will only give back control to caller after finishing its compression job.
1237
- */
1238
- static size_t ZSTDMT_compress_advanced_internal(
1239
- ZSTDMT_CCtx* mtctx,
1240
- void* dst, size_t dstCapacity,
1241
- const void* src, size_t srcSize,
1242
- const ZSTD_CDict* cdict,
1243
- ZSTD_CCtx_params params)
1244
- {
1245
- ZSTD_CCtx_params const jobParams = ZSTDMT_initJobCCtxParams(&params);
1246
- size_t const overlapSize = ZSTDMT_computeOverlapSize(&params);
1247
- unsigned const nbJobs = ZSTDMT_computeNbJobs(&params, srcSize, params.nbWorkers);
1248
- size_t const proposedJobSize = (srcSize + (nbJobs-1)) / nbJobs;
1249
- size_t const avgJobSize = (((proposedJobSize-1) & 0x1FFFF) < 0x7FFF) ? proposedJobSize + 0xFFFF : proposedJobSize; /* avoid too small last block */
1250
- const char* const srcStart = (const char*)src;
1251
- size_t remainingSrcSize = srcSize;
1252
- unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbJobs : (unsigned)(dstCapacity / ZSTD_compressBound(avgJobSize)); /* presumes avgJobSize >= 256 KB, which should be the case */
1253
- size_t frameStartPos = 0, dstBufferPos = 0;
1254
- assert(jobParams.nbWorkers == 0);
1255
- assert(mtctx->cctxPool->totalCCtx == params.nbWorkers);
1256
-
1257
- params.jobSize = (U32)avgJobSize;
1258
- DEBUGLOG(4, "ZSTDMT_compress_advanced_internal: nbJobs=%2u (rawSize=%u bytes; fixedSize=%u) ",
1259
- nbJobs, (U32)proposedJobSize, (U32)avgJobSize);
1260
-
1261
- if ((nbJobs==1) | (params.nbWorkers<=1)) { /* fallback to single-thread mode : this is a blocking invocation anyway */
1262
- ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0];
1263
- DEBUGLOG(4, "ZSTDMT_compress_advanced_internal: fallback to single-thread mode");
1264
- if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, jobParams.fParams);
1265
- return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, &jobParams);
1266
- }
1267
-
1268
- assert(avgJobSize >= 256 KB); /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), required to compress directly into Dst (no additional buffer) */
1269
- ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(avgJobSize) );
1270
- if (ZSTDMT_serialState_reset(&mtctx->serial, mtctx->seqPool, params, avgJobSize))
1271
- return ERROR(memory_allocation);
1272
-
1273
- FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbJobs) ); /* only expands if necessary */
1274
-
1275
- { unsigned u;
1276
- for (u=0; u<nbJobs; u++) {
1277
- size_t const jobSize = MIN(remainingSrcSize, avgJobSize);
1278
- size_t const dstBufferCapacity = ZSTD_compressBound(jobSize);
1279
- buffer_t const dstAsBuffer = { (char*)dst + dstBufferPos, dstBufferCapacity };
1280
- buffer_t const dstBuffer = u < compressWithinDst ? dstAsBuffer : g_nullBuffer;
1281
- size_t dictSize = u ? overlapSize : 0;
1282
-
1283
- mtctx->jobs[u].prefix.start = srcStart + frameStartPos - dictSize;
1284
- mtctx->jobs[u].prefix.size = dictSize;
1285
- mtctx->jobs[u].src.start = srcStart + frameStartPos;
1286
- mtctx->jobs[u].src.size = jobSize; assert(jobSize > 0); /* avoid job.src.size == 0 */
1287
- mtctx->jobs[u].consumed = 0;
1288
- mtctx->jobs[u].cSize = 0;
1289
- mtctx->jobs[u].cdict = (u==0) ? cdict : NULL;
1290
- mtctx->jobs[u].fullFrameSize = srcSize;
1291
- mtctx->jobs[u].params = jobParams;
1292
- /* do not calculate checksum within sections, but write it in header for first section */
1293
- mtctx->jobs[u].dstBuff = dstBuffer;
1294
- mtctx->jobs[u].cctxPool = mtctx->cctxPool;
1295
- mtctx->jobs[u].bufPool = mtctx->bufPool;
1296
- mtctx->jobs[u].seqPool = mtctx->seqPool;
1297
- mtctx->jobs[u].serial = &mtctx->serial;
1298
- mtctx->jobs[u].jobID = u;
1299
- mtctx->jobs[u].firstJob = (u==0);
1300
- mtctx->jobs[u].lastJob = (u==nbJobs-1);
1301
-
1302
- DEBUGLOG(5, "ZSTDMT_compress_advanced_internal: posting job %u (%u bytes)", u, (U32)jobSize);
1303
- DEBUG_PRINTHEX(6, mtctx->jobs[u].prefix.start, 12);
1304
- POOL_add(mtctx->factory, ZSTDMT_compressionJob, &mtctx->jobs[u]);
1305
-
1306
- frameStartPos += jobSize;
1307
- dstBufferPos += dstBufferCapacity;
1308
- remainingSrcSize -= jobSize;
1309
- } }
1310
-
1311
- /* collect result */
1312
- { size_t error = 0, dstPos = 0;
1313
- unsigned jobID;
1314
- for (jobID=0; jobID<nbJobs; jobID++) {
1315
- DEBUGLOG(5, "waiting for job %u ", jobID);
1316
- ZSTD_PTHREAD_MUTEX_LOCK(&mtctx->jobs[jobID].job_mutex);
1317
- while (mtctx->jobs[jobID].consumed < mtctx->jobs[jobID].src.size) {
1318
- DEBUGLOG(5, "waiting for jobCompleted signal from job %u", jobID);
1319
- ZSTD_pthread_cond_wait(&mtctx->jobs[jobID].job_cond, &mtctx->jobs[jobID].job_mutex);
1320
- }
1321
- ZSTD_pthread_mutex_unlock(&mtctx->jobs[jobID].job_mutex);
1322
- DEBUGLOG(5, "ready to write job %u ", jobID);
1323
-
1324
- { size_t const cSize = mtctx->jobs[jobID].cSize;
1325
- if (ZSTD_isError(cSize)) error = cSize;
1326
- if ((!error) && (dstPos + cSize > dstCapacity)) error = ERROR(dstSize_tooSmall);
1327
- if (jobID) { /* note : job 0 is written directly at dst, which is correct position */
1328
- if (!error)
1329
- memmove((char*)dst + dstPos, mtctx->jobs[jobID].dstBuff.start, cSize); /* may overlap when job compressed within dst */
1330
- if (jobID >= compressWithinDst) { /* job compressed into its own buffer, which must be released */
1331
- DEBUGLOG(5, "releasing buffer %u>=%u", jobID, compressWithinDst);
1332
- ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
1333
- } }
1334
- mtctx->jobs[jobID].dstBuff = g_nullBuffer;
1335
- mtctx->jobs[jobID].cSize = 0;
1336
- dstPos += cSize ;
1337
- }
1338
- } /* for (jobID=0; jobID<nbJobs; jobID++) */
1339
-
1340
- DEBUGLOG(4, "checksumFlag : %u ", params.fParams.checksumFlag);
1341
- if (params.fParams.checksumFlag) {
1342
- U32 const checksum = (U32)XXH64_digest(&mtctx->serial.xxhState);
1343
- if (dstPos + 4 > dstCapacity) {
1344
- error = ERROR(dstSize_tooSmall);
1345
- } else {
1346
- DEBUGLOG(4, "writing checksum : %08X \n", checksum);
1347
- MEM_writeLE32((char*)dst + dstPos, checksum);
1348
- dstPos += 4;
1349
- } }
1350
-
1351
- if (!error) DEBUGLOG(4, "compressed size : %u ", (U32)dstPos);
1352
- return error ? error : dstPos;
1353
- }
1354
- }
1355
-
1356
- size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx,
1357
- void* dst, size_t dstCapacity,
1358
- const void* src, size_t srcSize,
1359
- const ZSTD_CDict* cdict,
1360
- ZSTD_parameters params,
1361
- int overlapLog)
1362
- {
1363
- ZSTD_CCtx_params cctxParams = mtctx->params;
1364
- cctxParams.cParams = params.cParams;
1365
- cctxParams.fParams = params.fParams;
1366
- assert(ZSTD_OVERLAPLOG_MIN <= overlapLog && overlapLog <= ZSTD_OVERLAPLOG_MAX);
1367
- cctxParams.overlapLog = overlapLog;
1368
- return ZSTDMT_compress_advanced_internal(mtctx,
1369
- dst, dstCapacity,
1370
- src, srcSize,
1371
- cdict, cctxParams);
1372
- }
1373
-
1374
-
1375
- size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
1376
- void* dst, size_t dstCapacity,
1377
- const void* src, size_t srcSize,
1378
- int compressionLevel)
1379
- {
1380
- ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, 0);
1381
- int const overlapLog = ZSTDMT_overlapLog_default(params.cParams.strategy);
1382
- params.fParams.contentSizeFlag = 1;
1383
- return ZSTDMT_compress_advanced(mtctx, dst, dstCapacity, src, srcSize, NULL, params, overlapLog);
1384
- }
1385
-
1386
-
1387
1213
  /* ====================================== */
1388
1214
  /* ======= Streaming API ======= */
1389
1215
  /* ====================================== */
@@ -1403,21 +1229,11 @@ size_t ZSTDMT_initCStream_internal(
1403
1229
 
1404
1230
  /* init */
1405
1231
  if (params.nbWorkers != mtctx->params.nbWorkers)
1406
- FORWARD_IF_ERROR( ZSTDMT_resize(mtctx, params.nbWorkers) );
1232
+ FORWARD_IF_ERROR( ZSTDMT_resize(mtctx, params.nbWorkers) , "");
1407
1233
 
1408
1234
  if (params.jobSize != 0 && params.jobSize < ZSTDMT_JOBSIZE_MIN) params.jobSize = ZSTDMT_JOBSIZE_MIN;
1409
1235
  if (params.jobSize > (size_t)ZSTDMT_JOBSIZE_MAX) params.jobSize = (size_t)ZSTDMT_JOBSIZE_MAX;
1410
1236
 
1411
- mtctx->singleBlockingThread = (pledgedSrcSize <= ZSTDMT_JOBSIZE_MIN); /* do not trigger multi-threading when srcSize is too small */
1412
- if (mtctx->singleBlockingThread) {
1413
- ZSTD_CCtx_params const singleThreadParams = ZSTDMT_initJobCCtxParams(&params);
1414
- DEBUGLOG(5, "ZSTDMT_initCStream_internal: switch to single blocking thread mode");
1415
- assert(singleThreadParams.nbWorkers == 0);
1416
- return ZSTD_initCStream_internal(mtctx->cctxPool->cctx[0],
1417
- dict, dictSize, cdict,
1418
- &singleThreadParams, pledgedSrcSize);
1419
- }
1420
-
1421
1237
  DEBUGLOG(4, "ZSTDMT_initCStream_internal: %u workers", params.nbWorkers);
1422
1238
 
1423
1239
  if (mtctx->allJobsCompleted == 0) { /* previous compression not correctly finished */
@@ -1451,9 +1267,11 @@ size_t ZSTDMT_initCStream_internal(
1451
1267
 
1452
1268
  if (params.rsyncable) {
1453
1269
  /* Aim for the targetsectionSize as the average job size. */
1454
- U32 const jobSizeMB = (U32)(mtctx->targetSectionSize >> 20);
1455
- U32 const rsyncBits = ZSTD_highbit32(jobSizeMB) + 20;
1456
- assert(jobSizeMB >= 1);
1270
+ U32 const jobSizeKB = (U32)(mtctx->targetSectionSize >> 10);
1271
+ U32 const rsyncBits = (assert(jobSizeKB >= 1), ZSTD_highbit32(jobSizeKB) + 10);
1272
+ /* We refuse to create jobs < RSYNC_MIN_BLOCK_SIZE bytes, so make sure our
1273
+ * expected job size is at least 4x larger. */
1274
+ assert(rsyncBits >= RSYNC_MIN_BLOCK_LOG + 2);
1457
1275
  DEBUGLOG(4, "rsyncLog = %u", rsyncBits);
1458
1276
  mtctx->rsync.hash = 0;
1459
1277
  mtctx->rsync.hitMask = (1ULL << rsyncBits) - 1;
@@ -1465,7 +1283,7 @@ size_t ZSTDMT_initCStream_internal(
1465
1283
  ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(mtctx->targetSectionSize));
1466
1284
  {
1467
1285
  /* If ldm is enabled we need windowSize space. */
1468
- size_t const windowSize = mtctx->params.ldmParams.enableLdm ? (1U << mtctx->params.cParams.windowLog) : 0;
1286
+ size_t const windowSize = mtctx->params.ldmParams.enableLdm == ZSTD_ps_enable ? (1U << mtctx->params.cParams.windowLog) : 0;
1469
1287
  /* Two buffers of slack, plus extra space for the overlap
1470
1288
  * This is the minimum slack that LDM works with. One extra because
1471
1289
  * flush might waste up to targetSectionSize-1 bytes. Another extra
@@ -1480,8 +1298,8 @@ size_t ZSTDMT_initCStream_internal(
1480
1298
  size_t const capacity = MAX(windowSize, sectionsSize) + slackSize;
1481
1299
  if (mtctx->roundBuff.capacity < capacity) {
1482
1300
  if (mtctx->roundBuff.buffer)
1483
- ZSTD_free(mtctx->roundBuff.buffer, mtctx->cMem);
1484
- mtctx->roundBuff.buffer = (BYTE*)ZSTD_malloc(capacity, mtctx->cMem);
1301
+ ZSTD_customFree(mtctx->roundBuff.buffer, mtctx->cMem);
1302
+ mtctx->roundBuff.buffer = (BYTE*)ZSTD_customMalloc(capacity, mtctx->cMem);
1485
1303
  if (mtctx->roundBuff.buffer == NULL) {
1486
1304
  mtctx->roundBuff.capacity = 0;
1487
1305
  return ERROR(memory_allocation);
@@ -1500,58 +1318,12 @@ size_t ZSTDMT_initCStream_internal(
1500
1318
  mtctx->allJobsCompleted = 0;
1501
1319
  mtctx->consumed = 0;
1502
1320
  mtctx->produced = 0;
1503
- if (ZSTDMT_serialState_reset(&mtctx->serial, mtctx->seqPool, params, mtctx->targetSectionSize))
1321
+ if (ZSTDMT_serialState_reset(&mtctx->serial, mtctx->seqPool, params, mtctx->targetSectionSize,
1322
+ dict, dictSize, dictContentType))
1504
1323
  return ERROR(memory_allocation);
1505
1324
  return 0;
1506
1325
  }
1507
1326
 
1508
- size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx,
1509
- const void* dict, size_t dictSize,
1510
- ZSTD_parameters params,
1511
- unsigned long long pledgedSrcSize)
1512
- {
1513
- ZSTD_CCtx_params cctxParams = mtctx->params; /* retrieve sticky params */
1514
- DEBUGLOG(4, "ZSTDMT_initCStream_advanced (pledgedSrcSize=%u)", (U32)pledgedSrcSize);
1515
- cctxParams.cParams = params.cParams;
1516
- cctxParams.fParams = params.fParams;
1517
- return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, ZSTD_dct_auto, NULL,
1518
- cctxParams, pledgedSrcSize);
1519
- }
1520
-
1521
- size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx,
1522
- const ZSTD_CDict* cdict,
1523
- ZSTD_frameParameters fParams,
1524
- unsigned long long pledgedSrcSize)
1525
- {
1526
- ZSTD_CCtx_params cctxParams = mtctx->params;
1527
- if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */
1528
- cctxParams.cParams = ZSTD_getCParamsFromCDict(cdict);
1529
- cctxParams.fParams = fParams;
1530
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, ZSTD_dct_auto, cdict,
1531
- cctxParams, pledgedSrcSize);
1532
- }
1533
-
1534
-
1535
- /* ZSTDMT_resetCStream() :
1536
- * pledgedSrcSize can be zero == unknown (for the time being)
1537
- * prefer using ZSTD_CONTENTSIZE_UNKNOWN,
1538
- * as `0` might mean "empty" in the future */
1539
- size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* mtctx, unsigned long long pledgedSrcSize)
1540
- {
1541
- if (!pledgedSrcSize) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN;
1542
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0, ZSTD_dct_auto, 0, mtctx->params,
1543
- pledgedSrcSize);
1544
- }
1545
-
1546
- size_t ZSTDMT_initCStream(ZSTDMT_CCtx* mtctx, int compressionLevel) {
1547
- ZSTD_parameters const params = ZSTD_getParams(compressionLevel, ZSTD_CONTENTSIZE_UNKNOWN, 0);
1548
- ZSTD_CCtx_params cctxParams = mtctx->params; /* retrieve sticky params */
1549
- DEBUGLOG(4, "ZSTDMT_initCStream (cLevel=%i)", compressionLevel);
1550
- cctxParams.cParams = params.cParams;
1551
- cctxParams.fParams = params.fParams;
1552
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0, ZSTD_dct_auto, NULL, cctxParams, ZSTD_CONTENTSIZE_UNKNOWN);
1553
- }
1554
-
1555
1327
 
1556
1328
  /* ZSTDMT_writeLastEmptyBlock()
1557
1329
  * Write a single empty block with an end-of-frame to finish a frame.
@@ -1714,9 +1486,11 @@ static size_t ZSTDMT_flushProduced(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, u
1714
1486
  assert(mtctx->doneJobID < mtctx->nextJobID);
1715
1487
  assert(cSize >= mtctx->jobs[wJobID].dstFlushed);
1716
1488
  assert(mtctx->jobs[wJobID].dstBuff.start != NULL);
1717
- memcpy((char*)output->dst + output->pos,
1718
- (const char*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].dstFlushed,
1719
- toFlush);
1489
+ if (toFlush > 0) {
1490
+ ZSTD_memcpy((char*)output->dst + output->pos,
1491
+ (const char*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].dstFlushed,
1492
+ toFlush);
1493
+ }
1720
1494
  output->pos += toFlush;
1721
1495
  mtctx->jobs[wJobID].dstFlushed += toFlush; /* can write : this value is only used by mtctx */
1722
1496
 
@@ -1784,17 +1558,21 @@ static range_t ZSTDMT_getInputDataInUse(ZSTDMT_CCtx* mtctx)
1784
1558
  static int ZSTDMT_isOverlapped(buffer_t buffer, range_t range)
1785
1559
  {
1786
1560
  BYTE const* const bufferStart = (BYTE const*)buffer.start;
1787
- BYTE const* const bufferEnd = bufferStart + buffer.capacity;
1788
1561
  BYTE const* const rangeStart = (BYTE const*)range.start;
1789
- BYTE const* const rangeEnd = rangeStart + range.size;
1790
1562
 
1791
1563
  if (rangeStart == NULL || bufferStart == NULL)
1792
1564
  return 0;
1793
- /* Empty ranges cannot overlap */
1794
- if (bufferStart == bufferEnd || rangeStart == rangeEnd)
1795
- return 0;
1796
1565
 
1797
- return bufferStart < rangeEnd && rangeStart < bufferEnd;
1566
+ {
1567
+ BYTE const* const bufferEnd = bufferStart + buffer.capacity;
1568
+ BYTE const* const rangeEnd = rangeStart + range.size;
1569
+
1570
+ /* Empty ranges cannot overlap */
1571
+ if (bufferStart == bufferEnd || rangeStart == rangeEnd)
1572
+ return 0;
1573
+
1574
+ return bufferStart < rangeEnd && rangeStart < bufferEnd;
1575
+ }
1798
1576
  }
1799
1577
 
1800
1578
  static int ZSTDMT_doesOverlapWindow(buffer_t buffer, ZSTD_window_t window)
@@ -1821,7 +1599,7 @@ static int ZSTDMT_doesOverlapWindow(buffer_t buffer, ZSTD_window_t window)
1821
1599
 
1822
1600
  static void ZSTDMT_waitForLdmComplete(ZSTDMT_CCtx* mtctx, buffer_t buffer)
1823
1601
  {
1824
- if (mtctx->params.ldmParams.enableLdm) {
1602
+ if (mtctx->params.ldmParams.enableLdm == ZSTD_ps_enable) {
1825
1603
  ZSTD_pthread_mutex_t* mutex = &mtctx->serial.ldmWindowMutex;
1826
1604
  DEBUGLOG(5, "ZSTDMT_waitForLdmComplete");
1827
1605
  DEBUGLOG(5, "source [0x%zx, 0x%zx)",
@@ -1867,7 +1645,7 @@ static int ZSTDMT_tryGetInputRange(ZSTDMT_CCtx* mtctx)
1867
1645
  return 0;
1868
1646
  }
1869
1647
  ZSTDMT_waitForLdmComplete(mtctx, buffer);
1870
- memmove(start, mtctx->inBuff.prefix.start, prefixSize);
1648
+ ZSTD_memmove(start, mtctx->inBuff.prefix.start, prefixSize);
1871
1649
  mtctx->inBuff.prefix.start = start;
1872
1650
  mtctx->roundBuff.pos = prefixSize;
1873
1651
  }
@@ -1924,6 +1702,11 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1924
1702
  if (!mtctx->params.rsyncable)
1925
1703
  /* Rsync is disabled. */
1926
1704
  return syncPoint;
1705
+ if (mtctx->inBuff.filled + input.size - input.pos < RSYNC_MIN_BLOCK_SIZE)
1706
+ /* We don't emit synchronization points if it would produce too small blocks.
1707
+ * We don't have enough input to find a synchronization point, so don't look.
1708
+ */
1709
+ return syncPoint;
1927
1710
  if (mtctx->inBuff.filled + syncPoint.toLoad < RSYNC_LENGTH)
1928
1711
  /* Not enough to compute the hash.
1929
1712
  * We will miss any synchronization points in this RSYNC_LENGTH byte
@@ -1934,23 +1717,41 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1934
1717
  */
1935
1718
  return syncPoint;
1936
1719
  /* Initialize the loop variables. */
1937
- if (mtctx->inBuff.filled >= RSYNC_LENGTH) {
1938
- /* We have enough bytes buffered to initialize the hash.
1720
+ if (mtctx->inBuff.filled < RSYNC_MIN_BLOCK_SIZE) {
1721
+ /* We don't need to scan the first RSYNC_MIN_BLOCK_SIZE positions
1722
+ * because they can't possibly be a sync point. So we can start
1723
+ * part way through the input buffer.
1724
+ */
1725
+ pos = RSYNC_MIN_BLOCK_SIZE - mtctx->inBuff.filled;
1726
+ if (pos >= RSYNC_LENGTH) {
1727
+ prev = istart + pos - RSYNC_LENGTH;
1728
+ hash = ZSTD_rollingHash_compute(prev, RSYNC_LENGTH);
1729
+ } else {
1730
+ assert(mtctx->inBuff.filled >= RSYNC_LENGTH);
1731
+ prev = (BYTE const*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled - RSYNC_LENGTH;
1732
+ hash = ZSTD_rollingHash_compute(prev + pos, (RSYNC_LENGTH - pos));
1733
+ hash = ZSTD_rollingHash_append(hash, istart, pos);
1734
+ }
1735
+ } else {
1736
+ /* We have enough bytes buffered to initialize the hash,
1737
+ * and are have processed enough bytes to find a sync point.
1939
1738
  * Start scanning at the beginning of the input.
1940
1739
  */
1740
+ assert(mtctx->inBuff.filled >= RSYNC_MIN_BLOCK_SIZE);
1741
+ assert(RSYNC_MIN_BLOCK_SIZE >= RSYNC_LENGTH);
1941
1742
  pos = 0;
1942
1743
  prev = (BYTE const*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled - RSYNC_LENGTH;
1943
1744
  hash = ZSTD_rollingHash_compute(prev, RSYNC_LENGTH);
1944
- } else {
1945
- /* We don't have enough bytes buffered to initialize the hash, but
1946
- * we know we have at least RSYNC_LENGTH bytes total.
1947
- * Start scanning after the first RSYNC_LENGTH bytes less the bytes
1948
- * already buffered.
1949
- */
1950
- pos = RSYNC_LENGTH - mtctx->inBuff.filled;
1951
- prev = (BYTE const*)mtctx->inBuff.buffer.start - pos;
1952
- hash = ZSTD_rollingHash_compute(mtctx->inBuff.buffer.start, mtctx->inBuff.filled);
1953
- hash = ZSTD_rollingHash_append(hash, istart, pos);
1745
+ if ((hash & hitMask) == hitMask) {
1746
+ /* We're already at a sync point so don't load any more until
1747
+ * we're able to flush this sync point.
1748
+ * This likely happened because the job table was full so we
1749
+ * couldn't add our job.
1750
+ */
1751
+ syncPoint.toLoad = 0;
1752
+ syncPoint.flush = 1;
1753
+ return syncPoint;
1754
+ }
1954
1755
  }
1955
1756
  /* Starting with the hash of the previous RSYNC_LENGTH bytes, roll
1956
1757
  * through the input. If we hit a synchronization point, then cut the
@@ -1962,8 +1763,9 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1962
1763
  */
1963
1764
  for (; pos < syncPoint.toLoad; ++pos) {
1964
1765
  BYTE const toRemove = pos < RSYNC_LENGTH ? prev[pos] : istart[pos - RSYNC_LENGTH];
1965
- /* if (pos >= RSYNC_LENGTH) assert(ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash); */
1766
+ assert(pos < RSYNC_LENGTH || ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash);
1966
1767
  hash = ZSTD_rollingHash_rotate(hash, toRemove, istart[pos], primePower);
1768
+ assert(mtctx->inBuff.filled + pos >= RSYNC_MIN_BLOCK_SIZE);
1967
1769
  if ((hash & hitMask) == hitMask) {
1968
1770
  syncPoint.toLoad = pos + 1;
1969
1771
  syncPoint.flush = 1;
@@ -1995,34 +1797,11 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
1995
1797
  assert(output->pos <= output->size);
1996
1798
  assert(input->pos <= input->size);
1997
1799
 
1998
- if (mtctx->singleBlockingThread) { /* delegate to single-thread (synchronous) */
1999
- return ZSTD_compressStream2(mtctx->cctxPool->cctx[0], output, input, endOp);
2000
- }
2001
-
2002
1800
  if ((mtctx->frameEnded) && (endOp==ZSTD_e_continue)) {
2003
1801
  /* current frame being ended. Only flush/end are allowed */
2004
1802
  return ERROR(stage_wrong);
2005
1803
  }
2006
1804
 
2007
- /* single-pass shortcut (note : synchronous-mode) */
2008
- if ( (!mtctx->params.rsyncable) /* rsyncable mode is disabled */
2009
- && (mtctx->nextJobID == 0) /* just started */
2010
- && (mtctx->inBuff.filled == 0) /* nothing buffered */
2011
- && (!mtctx->jobReady) /* no job already created */
2012
- && (endOp == ZSTD_e_end) /* end order */
2013
- && (output->size - output->pos >= ZSTD_compressBound(input->size - input->pos)) ) { /* enough space in dst */
2014
- size_t const cSize = ZSTDMT_compress_advanced_internal(mtctx,
2015
- (char*)output->dst + output->pos, output->size - output->pos,
2016
- (const char*)input->src + input->pos, input->size - input->pos,
2017
- mtctx->cdict, mtctx->params);
2018
- if (ZSTD_isError(cSize)) return cSize;
2019
- input->pos = input->size;
2020
- output->pos += cSize;
2021
- mtctx->allJobsCompleted = 1;
2022
- mtctx->frameEnded = 1;
2023
- return 0;
2024
- }
2025
-
2026
1805
  /* fill input buffer */
2027
1806
  if ( (!mtctx->jobReady)
2028
1807
  && (input->size > input->pos) ) { /* support NULL input */
@@ -2045,13 +1824,21 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2045
1824
  assert(mtctx->inBuff.buffer.capacity >= mtctx->targetSectionSize);
2046
1825
  DEBUGLOG(5, "ZSTDMT_compressStream_generic: adding %u bytes on top of %u to buffer of size %u",
2047
1826
  (U32)syncPoint.toLoad, (U32)mtctx->inBuff.filled, (U32)mtctx->targetSectionSize);
2048
- memcpy((char*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, (const char*)input->src + input->pos, syncPoint.toLoad);
1827
+ ZSTD_memcpy((char*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, (const char*)input->src + input->pos, syncPoint.toLoad);
2049
1828
  input->pos += syncPoint.toLoad;
2050
1829
  mtctx->inBuff.filled += syncPoint.toLoad;
2051
1830
  forwardInputProgress = syncPoint.toLoad>0;
2052
1831
  }
2053
- if ((input->pos < input->size) && (endOp == ZSTD_e_end))
2054
- endOp = ZSTD_e_flush; /* can't end now : not all input consumed */
1832
+ }
1833
+ if ((input->pos < input->size) && (endOp == ZSTD_e_end)) {
1834
+ /* Can't end yet because the input is not fully consumed.
1835
+ * We are in one of these cases:
1836
+ * - mtctx->inBuff is NULL & empty: we couldn't get an input buffer so don't create a new job.
1837
+ * - We filled the input buffer: flush this job but don't end the frame.
1838
+ * - We hit a synchronization point: flush this job but don't end the frame.
1839
+ */
1840
+ assert(mtctx->inBuff.filled == 0 || mtctx->inBuff.filled == mtctx->targetSectionSize || mtctx->params.rsyncable);
1841
+ endOp = ZSTD_e_flush;
2055
1842
  }
2056
1843
 
2057
1844
  if ( (mtctx->jobReady)
@@ -2060,7 +1847,7 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2060
1847
  || ((endOp == ZSTD_e_end) && (!mtctx->frameEnded)) ) { /* must finish the frame with a zero-size block */
2061
1848
  size_t const jobSize = mtctx->inBuff.filled;
2062
1849
  assert(mtctx->inBuff.filled <= mtctx->targetSectionSize);
2063
- FORWARD_IF_ERROR( ZSTDMT_createCompressionJob(mtctx, jobSize, endOp) );
1850
+ FORWARD_IF_ERROR( ZSTDMT_createCompressionJob(mtctx, jobSize, endOp) , "");
2064
1851
  }
2065
1852
 
2066
1853
  /* check for potential compressed data ready to be flushed */
@@ -2070,47 +1857,3 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2070
1857
  return remainingToFlush;
2071
1858
  }
2072
1859
  }
2073
-
2074
-
2075
- size_t ZSTDMT_compressStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
2076
- {
2077
- FORWARD_IF_ERROR( ZSTDMT_compressStream_generic(mtctx, output, input, ZSTD_e_continue) );
2078
-
2079
- /* recommended next input size : fill current input buffer */
2080
- return mtctx->targetSectionSize - mtctx->inBuff.filled; /* note : could be zero when input buffer is fully filled and no more availability to create new job */
2081
- }
2082
-
2083
-
2084
- static size_t ZSTDMT_flushStream_internal(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_EndDirective endFrame)
2085
- {
2086
- size_t const srcSize = mtctx->inBuff.filled;
2087
- DEBUGLOG(5, "ZSTDMT_flushStream_internal");
2088
-
2089
- if ( mtctx->jobReady /* one job ready for a worker to pick up */
2090
- || (srcSize > 0) /* still some data within input buffer */
2091
- || ((endFrame==ZSTD_e_end) && !mtctx->frameEnded)) { /* need a last 0-size block to end frame */
2092
- DEBUGLOG(5, "ZSTDMT_flushStream_internal : create a new job (%u bytes, end:%u)",
2093
- (U32)srcSize, (U32)endFrame);
2094
- FORWARD_IF_ERROR( ZSTDMT_createCompressionJob(mtctx, srcSize, endFrame) );
2095
- }
2096
-
2097
- /* check if there is any data available to flush */
2098
- return ZSTDMT_flushProduced(mtctx, output, 1 /* blockToFlush */, endFrame);
2099
- }
2100
-
2101
-
2102
- size_t ZSTDMT_flushStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output)
2103
- {
2104
- DEBUGLOG(5, "ZSTDMT_flushStream");
2105
- if (mtctx->singleBlockingThread)
2106
- return ZSTD_flushStream(mtctx->cctxPool->cctx[0], output);
2107
- return ZSTDMT_flushStream_internal(mtctx, output, ZSTD_e_flush);
2108
- }
2109
-
2110
- size_t ZSTDMT_endStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output)
2111
- {
2112
- DEBUGLOG(4, "ZSTDMT_endStream");
2113
- if (mtctx->singleBlockingThread)
2114
- return ZSTD_endStream(mtctx->cctxPool->cctx[0], output);
2115
- return ZSTDMT_flushStream_internal(mtctx, output, ZSTD_e_end);
2116
- }