zstd-ruby 1.4.5.0 → 1.4.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +35 -0
  3. data/README.md +2 -2
  4. data/ext/zstdruby/libzstd/Makefile +237 -138
  5. data/ext/zstdruby/libzstd/README.md +28 -0
  6. data/ext/zstdruby/libzstd/common/bitstream.h +25 -16
  7. data/ext/zstdruby/libzstd/common/compiler.h +118 -4
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  9. data/ext/zstdruby/libzstd/common/debug.c +1 -1
  10. data/ext/zstdruby/libzstd/common/debug.h +12 -19
  11. data/ext/zstdruby/libzstd/common/entropy_common.c +189 -43
  12. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  13. data/ext/zstdruby/libzstd/common/error_private.h +2 -2
  14. data/ext/zstdruby/libzstd/common/fse.h +40 -12
  15. data/ext/zstdruby/libzstd/common/fse_decompress.c +124 -17
  16. data/ext/zstdruby/libzstd/common/huf.h +27 -6
  17. data/ext/zstdruby/libzstd/common/mem.h +67 -94
  18. data/ext/zstdruby/libzstd/common/pool.c +23 -17
  19. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  20. data/ext/zstdruby/libzstd/common/threading.c +6 -5
  21. data/ext/zstdruby/libzstd/common/xxhash.c +19 -57
  22. data/ext/zstdruby/libzstd/common/xxhash.h +2 -2
  23. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  24. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  25. data/ext/zstdruby/libzstd/common/zstd_errors.h +2 -1
  26. data/ext/zstdruby/libzstd/common/zstd_internal.h +90 -59
  27. data/ext/zstdruby/libzstd/common/zstd_trace.c +42 -0
  28. data/ext/zstdruby/libzstd/common/zstd_trace.h +152 -0
  29. data/ext/zstdruby/libzstd/compress/fse_compress.c +31 -24
  30. data/ext/zstdruby/libzstd/compress/hist.c +27 -29
  31. data/ext/zstdruby/libzstd/compress/hist.h +2 -2
  32. data/ext/zstdruby/libzstd/compress/huf_compress.c +217 -101
  33. data/ext/zstdruby/libzstd/compress/zstd_compress.c +1495 -478
  34. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +143 -44
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +7 -7
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +1 -1
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +18 -4
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +1 -1
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +25 -21
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +1 -1
  41. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +62 -26
  42. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +23 -23
  43. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +1 -1
  44. data/ext/zstdruby/libzstd/compress/zstd_fast.c +21 -21
  45. data/ext/zstdruby/libzstd/compress/zstd_fast.h +1 -1
  46. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +352 -78
  47. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +21 -1
  48. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +276 -209
  49. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +8 -2
  50. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +103 -0
  51. data/ext/zstdruby/libzstd/compress/zstd_opt.c +191 -46
  52. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  53. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +79 -410
  54. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +27 -109
  55. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +303 -201
  56. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +9 -9
  57. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  58. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +370 -87
  59. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +153 -45
  60. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +6 -3
  61. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +28 -11
  62. data/ext/zstdruby/libzstd/deprecated/zbuff.h +1 -1
  63. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +1 -1
  64. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  65. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  66. data/ext/zstdruby/libzstd/dictBuilder/cover.c +40 -31
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.h +2 -2
  68. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  69. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +26 -25
  70. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +22 -24
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +5 -4
  72. data/ext/zstdruby/libzstd/dll/example/Makefile +1 -1
  73. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  74. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +1 -1
  75. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +6 -2
  76. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  77. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +6 -2
  78. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  79. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +6 -2
  80. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  81. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +7 -3
  82. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  83. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +10 -6
  84. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +1 -1
  85. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +10 -6
  86. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  87. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +10 -6
  88. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  89. data/ext/zstdruby/libzstd/libzstd.pc.in +3 -3
  90. data/ext/zstdruby/libzstd/zstd.h +414 -54
  91. data/lib/zstd-ruby/version.rb +1 -1
  92. metadata +7 -3
  93. data/.travis.yml +0 -14
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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,8 +20,7 @@
20
20
 
21
21
 
22
22
  /* ====== Dependencies ====== */
23
- #include <string.h> /* memcpy, memset */
24
- #include <limits.h> /* INT_MAX, UINT_MAX */
23
+ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset, INT_MAX, UINT_MAX */
25
24
  #include "../common/mem.h" /* MEM_STATIC */
26
25
  #include "../common/pool.h" /* threadpool */
27
26
  #include "../common/threading.h" /* mutex */
@@ -106,11 +105,11 @@ typedef struct ZSTDMT_bufferPool_s {
106
105
  static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned nbWorkers, ZSTD_customMem cMem)
107
106
  {
108
107
  unsigned const maxNbBuffers = 2*nbWorkers + 3;
109
- ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)ZSTD_calloc(
108
+ ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)ZSTD_customCalloc(
110
109
  sizeof(ZSTDMT_bufferPool) + (maxNbBuffers-1) * sizeof(buffer_t), cMem);
111
110
  if (bufPool==NULL) return NULL;
112
111
  if (ZSTD_pthread_mutex_init(&bufPool->poolMutex, NULL)) {
113
- ZSTD_free(bufPool, cMem);
112
+ ZSTD_customFree(bufPool, cMem);
114
113
  return NULL;
115
114
  }
116
115
  bufPool->bufferSize = 64 KB;
@@ -127,10 +126,10 @@ static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool* bufPool)
127
126
  if (!bufPool) return; /* compatibility with free on NULL */
128
127
  for (u=0; u<bufPool->totalBuffers; u++) {
129
128
  DEBUGLOG(4, "free buffer %2u (address:%08X)", u, (U32)(size_t)bufPool->bTable[u].start);
130
- ZSTD_free(bufPool->bTable[u].start, bufPool->cMem);
129
+ ZSTD_customFree(bufPool->bTable[u].start, bufPool->cMem);
131
130
  }
132
131
  ZSTD_pthread_mutex_destroy(&bufPool->poolMutex);
133
- ZSTD_free(bufPool, bufPool->cMem);
132
+ ZSTD_customFree(bufPool, bufPool->cMem);
134
133
  }
135
134
 
136
135
  /* only works at initialization, not during compression */
@@ -201,13 +200,13 @@ static buffer_t ZSTDMT_getBuffer(ZSTDMT_bufferPool* bufPool)
201
200
  }
202
201
  /* size conditions not respected : scratch this buffer, create new one */
203
202
  DEBUGLOG(5, "ZSTDMT_getBuffer: existing buffer does not meet size conditions => freeing");
204
- ZSTD_free(buf.start, bufPool->cMem);
203
+ ZSTD_customFree(buf.start, bufPool->cMem);
205
204
  }
206
205
  ZSTD_pthread_mutex_unlock(&bufPool->poolMutex);
207
206
  /* create new buffer */
208
207
  DEBUGLOG(5, "ZSTDMT_getBuffer: create a new buffer");
209
208
  { buffer_t buffer;
210
- void* const start = ZSTD_malloc(bSize, bufPool->cMem);
209
+ void* const start = ZSTD_customMalloc(bSize, bufPool->cMem);
211
210
  buffer.start = start; /* note : start can be NULL if malloc fails ! */
212
211
  buffer.capacity = (start==NULL) ? 0 : bSize;
213
212
  if (start==NULL) {
@@ -229,13 +228,13 @@ static buffer_t ZSTDMT_resizeBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buffer)
229
228
  {
230
229
  size_t const bSize = bufPool->bufferSize;
231
230
  if (buffer.capacity < bSize) {
232
- void* const start = ZSTD_malloc(bSize, bufPool->cMem);
231
+ void* const start = ZSTD_customMalloc(bSize, bufPool->cMem);
233
232
  buffer_t newBuffer;
234
233
  newBuffer.start = start;
235
234
  newBuffer.capacity = start == NULL ? 0 : bSize;
236
235
  if (start != NULL) {
237
236
  assert(newBuffer.capacity >= buffer.capacity);
238
- memcpy(newBuffer.start, buffer.start, buffer.capacity);
237
+ ZSTD_memcpy(newBuffer.start, buffer.start, buffer.capacity);
239
238
  DEBUGLOG(5, "ZSTDMT_resizeBuffer: created buffer of size %u", (U32)bSize);
240
239
  return newBuffer;
241
240
  }
@@ -261,14 +260,12 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf)
261
260
  ZSTD_pthread_mutex_unlock(&bufPool->poolMutex);
262
261
  /* Reached bufferPool capacity (should not happen) */
263
262
  DEBUGLOG(5, "ZSTDMT_releaseBuffer: pool capacity reached => freeing ");
264
- ZSTD_free(buf.start, bufPool->cMem);
263
+ ZSTD_customFree(buf.start, bufPool->cMem);
265
264
  }
266
265
 
267
266
 
268
267
  /* ===== Seq Pool Wrapper ====== */
269
268
 
270
- static rawSeqStore_t kNullRawSeqStore = {NULL, 0, 0, 0};
271
-
272
269
  typedef ZSTDMT_bufferPool ZSTDMT_seqPool;
273
270
 
274
271
  static size_t ZSTDMT_sizeof_seqPool(ZSTDMT_seqPool* seqPool)
@@ -278,7 +275,7 @@ static size_t ZSTDMT_sizeof_seqPool(ZSTDMT_seqPool* seqPool)
278
275
 
279
276
  static rawSeqStore_t bufferToSeq(buffer_t buffer)
280
277
  {
281
- rawSeqStore_t seq = {NULL, 0, 0, 0};
278
+ rawSeqStore_t seq = kNullRawSeqStore;
282
279
  seq.seq = (rawSeq*)buffer.start;
283
280
  seq.capacity = buffer.capacity / sizeof(rawSeq);
284
281
  return seq;
@@ -354,7 +351,7 @@ static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
354
351
  for (cid=0; cid<pool->totalCCtx; cid++)
355
352
  ZSTD_freeCCtx(pool->cctx[cid]); /* note : compatible with free on NULL */
356
353
  ZSTD_pthread_mutex_destroy(&pool->poolMutex);
357
- ZSTD_free(pool, pool->cMem);
354
+ ZSTD_customFree(pool, pool->cMem);
358
355
  }
359
356
 
360
357
  /* ZSTDMT_createCCtxPool() :
@@ -362,12 +359,12 @@ static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
362
359
  static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(int nbWorkers,
363
360
  ZSTD_customMem cMem)
364
361
  {
365
- ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) ZSTD_calloc(
362
+ ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) ZSTD_customCalloc(
366
363
  sizeof(ZSTDMT_CCtxPool) + (nbWorkers-1)*sizeof(ZSTD_CCtx*), cMem);
367
364
  assert(nbWorkers > 0);
368
365
  if (!cctxPool) return NULL;
369
366
  if (ZSTD_pthread_mutex_init(&cctxPool->poolMutex, NULL)) {
370
- ZSTD_free(cctxPool, cMem);
367
+ ZSTD_customFree(cctxPool, cMem);
371
368
  return NULL;
372
369
  }
373
370
  cctxPool->cMem = cMem;
@@ -475,10 +472,8 @@ ZSTDMT_serialState_reset(serialState_t* serialState,
475
472
  ZSTD_ldm_adjustParameters(&params.ldmParams, &params.cParams);
476
473
  assert(params.ldmParams.hashLog >= params.ldmParams.bucketSizeLog);
477
474
  assert(params.ldmParams.hashRateLog < 32);
478
- serialState->ldmState.hashPower =
479
- ZSTD_rollingHash_primePower(params.ldmParams.minMatchLength);
480
475
  } else {
481
- memset(&params.ldmParams, 0, sizeof(params.ldmParams));
476
+ ZSTD_memset(&params.ldmParams, 0, sizeof(params.ldmParams));
482
477
  }
483
478
  serialState->nextJobID = 0;
484
479
  if (params.fParams.checksumFlag)
@@ -489,28 +484,28 @@ ZSTDMT_serialState_reset(serialState_t* serialState,
489
484
  size_t const hashSize = ((size_t)1 << hashLog) * sizeof(ldmEntry_t);
490
485
  unsigned const bucketLog =
491
486
  params.ldmParams.hashLog - params.ldmParams.bucketSizeLog;
492
- size_t const bucketSize = (size_t)1 << bucketLog;
493
487
  unsigned const prevBucketLog =
494
488
  serialState->params.ldmParams.hashLog -
495
489
  serialState->params.ldmParams.bucketSizeLog;
490
+ size_t const numBuckets = (size_t)1 << bucketLog;
496
491
  /* Size the seq pool tables */
497
492
  ZSTDMT_setNbSeq(seqPool, ZSTD_ldm_getMaxNbSeq(params.ldmParams, jobSize));
498
493
  /* Reset the window */
499
494
  ZSTD_window_init(&serialState->ldmState.window);
500
495
  /* Resize tables and output space if necessary. */
501
496
  if (serialState->ldmState.hashTable == NULL || serialState->params.ldmParams.hashLog < hashLog) {
502
- ZSTD_free(serialState->ldmState.hashTable, cMem);
503
- serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_malloc(hashSize, cMem);
497
+ ZSTD_customFree(serialState->ldmState.hashTable, cMem);
498
+ serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_customMalloc(hashSize, cMem);
504
499
  }
505
500
  if (serialState->ldmState.bucketOffsets == NULL || prevBucketLog < bucketLog) {
506
- ZSTD_free(serialState->ldmState.bucketOffsets, cMem);
507
- serialState->ldmState.bucketOffsets = (BYTE*)ZSTD_malloc(bucketSize, cMem);
501
+ ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem);
502
+ serialState->ldmState.bucketOffsets = (BYTE*)ZSTD_customMalloc(numBuckets, cMem);
508
503
  }
509
504
  if (!serialState->ldmState.hashTable || !serialState->ldmState.bucketOffsets)
510
505
  return 1;
511
506
  /* Zero the tables */
512
- memset(serialState->ldmState.hashTable, 0, hashSize);
513
- memset(serialState->ldmState.bucketOffsets, 0, bucketSize);
507
+ ZSTD_memset(serialState->ldmState.hashTable, 0, hashSize);
508
+ ZSTD_memset(serialState->ldmState.bucketOffsets, 0, numBuckets);
514
509
 
515
510
  /* Update window state and fill hash table with dict */
516
511
  serialState->ldmState.loadedDictEnd = 0;
@@ -537,7 +532,7 @@ ZSTDMT_serialState_reset(serialState_t* serialState,
537
532
  static int ZSTDMT_serialState_init(serialState_t* serialState)
538
533
  {
539
534
  int initError = 0;
540
- memset(serialState, 0, sizeof(*serialState));
535
+ ZSTD_memset(serialState, 0, sizeof(*serialState));
541
536
  initError |= ZSTD_pthread_mutex_init(&serialState->mutex, NULL);
542
537
  initError |= ZSTD_pthread_cond_init(&serialState->cond, NULL);
543
538
  initError |= ZSTD_pthread_mutex_init(&serialState->ldmWindowMutex, NULL);
@@ -552,8 +547,8 @@ static void ZSTDMT_serialState_free(serialState_t* serialState)
552
547
  ZSTD_pthread_cond_destroy(&serialState->cond);
553
548
  ZSTD_pthread_mutex_destroy(&serialState->ldmWindowMutex);
554
549
  ZSTD_pthread_cond_destroy(&serialState->ldmWindowCond);
555
- ZSTD_free(serialState->ldmState.hashTable, cMem);
556
- ZSTD_free(serialState->ldmState.bucketOffsets, cMem);
550
+ ZSTD_customFree(serialState->ldmState.hashTable, cMem);
551
+ ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem);
557
552
  }
558
553
 
559
554
  static void ZSTDMT_serialState_update(serialState_t* serialState,
@@ -686,6 +681,8 @@ static void ZSTDMT_compressionJob(void* jobDescription)
686
681
  if (job->jobID != 0) jobParams.fParams.checksumFlag = 0;
687
682
  /* Don't run LDM for the chunks, since we handle it externally */
688
683
  jobParams.ldmParams.enableLdm = 0;
684
+ /* Correct nbWorkers to 0. */
685
+ jobParams.nbWorkers = 0;
689
686
 
690
687
 
691
688
  /* init */
@@ -753,6 +750,7 @@ static void ZSTDMT_compressionJob(void* jobDescription)
753
750
  if (ZSTD_isError(cSize)) JOB_ERROR(cSize);
754
751
  lastCBlockSize = cSize;
755
752
  } }
753
+ ZSTD_CCtx_trace(cctx, 0);
756
754
 
757
755
  _endJob:
758
756
  ZSTDMT_serialState_ensureFinished(job->serial, job->jobID, job->cSize);
@@ -820,7 +818,6 @@ struct ZSTDMT_CCtx_s {
820
818
  roundBuff_t roundBuff;
821
819
  serialState_t serial;
822
820
  rsyncState_t rsync;
823
- unsigned singleBlockingThread;
824
821
  unsigned jobIDMask;
825
822
  unsigned doneJobID;
826
823
  unsigned nextJobID;
@@ -832,6 +829,7 @@ struct ZSTDMT_CCtx_s {
832
829
  ZSTD_customMem cMem;
833
830
  ZSTD_CDict* cdictLocal;
834
831
  const ZSTD_CDict* cdict;
832
+ unsigned providedFactory: 1;
835
833
  };
836
834
 
837
835
  static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, U32 nbJobs, ZSTD_customMem cMem)
@@ -842,7 +840,7 @@ static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, U32 nbJobs, ZS
842
840
  ZSTD_pthread_mutex_destroy(&jobTable[jobNb].job_mutex);
843
841
  ZSTD_pthread_cond_destroy(&jobTable[jobNb].job_cond);
844
842
  }
845
- ZSTD_free(jobTable, cMem);
843
+ ZSTD_customFree(jobTable, cMem);
846
844
  }
847
845
 
848
846
  /* ZSTDMT_allocJobsTable()
@@ -854,7 +852,7 @@ static ZSTDMT_jobDescription* ZSTDMT_createJobsTable(U32* nbJobsPtr, ZSTD_custom
854
852
  U32 const nbJobs = 1 << nbJobsLog2;
855
853
  U32 jobNb;
856
854
  ZSTDMT_jobDescription* const jobTable = (ZSTDMT_jobDescription*)
857
- ZSTD_calloc(nbJobs * sizeof(ZSTDMT_jobDescription), cMem);
855
+ ZSTD_customCalloc(nbJobs * sizeof(ZSTDMT_jobDescription), cMem);
858
856
  int initError = 0;
859
857
  if (jobTable==NULL) return NULL;
860
858
  *nbJobsPtr = nbJobs;
@@ -885,12 +883,12 @@ static size_t ZSTDMT_expandJobsTable (ZSTDMT_CCtx* mtctx, U32 nbWorkers) {
885
883
 
886
884
  /* ZSTDMT_CCtxParam_setNbWorkers():
887
885
  * Internal use only */
888
- size_t ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params* params, unsigned nbWorkers)
886
+ static size_t ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params* params, unsigned nbWorkers)
889
887
  {
890
888
  return ZSTD_CCtxParams_setParameter(params, ZSTD_c_nbWorkers, (int)nbWorkers);
891
889
  }
892
890
 
893
- MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers, ZSTD_customMem cMem)
891
+ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers, ZSTD_customMem cMem, ZSTD_threadPool* pool)
894
892
  {
895
893
  ZSTDMT_CCtx* mtctx;
896
894
  U32 nbJobs = nbWorkers + 2;
@@ -903,12 +901,19 @@ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers,
903
901
  /* invalid custom allocator */
904
902
  return NULL;
905
903
 
906
- mtctx = (ZSTDMT_CCtx*) ZSTD_calloc(sizeof(ZSTDMT_CCtx), cMem);
904
+ mtctx = (ZSTDMT_CCtx*) ZSTD_customCalloc(sizeof(ZSTDMT_CCtx), cMem);
907
905
  if (!mtctx) return NULL;
908
906
  ZSTDMT_CCtxParam_setNbWorkers(&mtctx->params, nbWorkers);
909
907
  mtctx->cMem = cMem;
910
908
  mtctx->allJobsCompleted = 1;
911
- mtctx->factory = POOL_create_advanced(nbWorkers, 0, cMem);
909
+ if (pool != NULL) {
910
+ mtctx->factory = pool;
911
+ mtctx->providedFactory = 1;
912
+ }
913
+ else {
914
+ mtctx->factory = POOL_create_advanced(nbWorkers, 0, cMem);
915
+ mtctx->providedFactory = 0;
916
+ }
912
917
  mtctx->jobs = ZSTDMT_createJobsTable(&nbJobs, cMem);
913
918
  assert(nbJobs > 0); assert((nbJobs & (nbJobs - 1)) == 0); /* ensure nbJobs is a power of 2 */
914
919
  mtctx->jobIDMask = nbJobs - 1;
@@ -925,22 +930,18 @@ MEM_STATIC ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced_internal(unsigned nbWorkers,
925
930
  return mtctx;
926
931
  }
927
932
 
928
- ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers, ZSTD_customMem cMem)
933
+ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers, ZSTD_customMem cMem, ZSTD_threadPool* pool)
929
934
  {
930
935
  #ifdef ZSTD_MULTITHREAD
931
- return ZSTDMT_createCCtx_advanced_internal(nbWorkers, cMem);
936
+ return ZSTDMT_createCCtx_advanced_internal(nbWorkers, cMem, pool);
932
937
  #else
933
938
  (void)nbWorkers;
934
939
  (void)cMem;
940
+ (void)pool;
935
941
  return NULL;
936
942
  #endif
937
943
  }
938
944
 
939
- ZSTDMT_CCtx* ZSTDMT_createCCtx(unsigned nbWorkers)
940
- {
941
- return ZSTDMT_createCCtx_advanced(nbWorkers, ZSTD_defaultCMem);
942
- }
943
-
944
945
 
945
946
  /* ZSTDMT_releaseAllJobResources() :
946
947
  * note : ensure all workers are killed first ! */
@@ -957,7 +958,7 @@ static void ZSTDMT_releaseAllJobResources(ZSTDMT_CCtx* mtctx)
957
958
  ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
958
959
 
959
960
  /* Clear the job description, but keep the mutex/cond */
960
- memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
961
+ ZSTD_memset(&mtctx->jobs[jobID], 0, sizeof(mtctx->jobs[jobID]));
961
962
  mtctx->jobs[jobID].job_mutex = mutex;
962
963
  mtctx->jobs[jobID].job_cond = cond;
963
964
  }
@@ -984,7 +985,8 @@ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* mtctx)
984
985
  size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
985
986
  {
986
987
  if (mtctx==NULL) return 0; /* compatible with free on NULL */
987
- POOL_free(mtctx->factory); /* stop and free worker threads */
988
+ if (!mtctx->providedFactory)
989
+ POOL_free(mtctx->factory); /* stop and free worker threads */
988
990
  ZSTDMT_releaseAllJobResources(mtctx); /* release job resources into pools first */
989
991
  ZSTDMT_freeJobsTable(mtctx->jobs, mtctx->jobIDMask+1, mtctx->cMem);
990
992
  ZSTDMT_freeBufferPool(mtctx->bufPool);
@@ -993,8 +995,8 @@ size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
993
995
  ZSTDMT_serialState_free(&mtctx->serial);
994
996
  ZSTD_freeCDict(mtctx->cdictLocal);
995
997
  if (mtctx->roundBuff.buffer)
996
- ZSTD_free(mtctx->roundBuff.buffer, mtctx->cMem);
997
- ZSTD_free(mtctx, mtctx->cMem);
998
+ ZSTD_customFree(mtctx->roundBuff.buffer, mtctx->cMem);
999
+ ZSTD_customFree(mtctx, mtctx->cMem);
998
1000
  return 0;
999
1001
  }
1000
1002
 
@@ -1011,65 +1013,6 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx)
1011
1013
  + mtctx->roundBuff.capacity;
1012
1014
  }
1013
1015
 
1014
- /* Internal only */
1015
- size_t
1016
- ZSTDMT_CCtxParam_setMTCtxParameter(ZSTD_CCtx_params* params,
1017
- ZSTDMT_parameter parameter,
1018
- int value)
1019
- {
1020
- DEBUGLOG(4, "ZSTDMT_CCtxParam_setMTCtxParameter");
1021
- switch(parameter)
1022
- {
1023
- case ZSTDMT_p_jobSize :
1024
- DEBUGLOG(4, "ZSTDMT_CCtxParam_setMTCtxParameter : set jobSize to %i", value);
1025
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_jobSize, value);
1026
- case ZSTDMT_p_overlapLog :
1027
- DEBUGLOG(4, "ZSTDMT_p_overlapLog : %i", value);
1028
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_overlapLog, value);
1029
- case ZSTDMT_p_rsyncable :
1030
- DEBUGLOG(4, "ZSTD_p_rsyncable : %i", value);
1031
- return ZSTD_CCtxParams_setParameter(params, ZSTD_c_rsyncable, value);
1032
- default :
1033
- return ERROR(parameter_unsupported);
1034
- }
1035
- }
1036
-
1037
- size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, int value)
1038
- {
1039
- DEBUGLOG(4, "ZSTDMT_setMTCtxParameter");
1040
- return ZSTDMT_CCtxParam_setMTCtxParameter(&mtctx->params, parameter, value);
1041
- }
1042
-
1043
- size_t ZSTDMT_getMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, int* value)
1044
- {
1045
- switch (parameter) {
1046
- case ZSTDMT_p_jobSize:
1047
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_jobSize, value);
1048
- case ZSTDMT_p_overlapLog:
1049
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_overlapLog, value);
1050
- case ZSTDMT_p_rsyncable:
1051
- return ZSTD_CCtxParams_getParameter(&mtctx->params, ZSTD_c_rsyncable, value);
1052
- default:
1053
- return ERROR(parameter_unsupported);
1054
- }
1055
- }
1056
-
1057
- /* Sets parameters relevant to the compression job,
1058
- * initializing others to default values. */
1059
- static ZSTD_CCtx_params ZSTDMT_initJobCCtxParams(const ZSTD_CCtx_params* params)
1060
- {
1061
- ZSTD_CCtx_params jobParams = *params;
1062
- /* Clear parameters related to multithreading */
1063
- jobParams.forceWindow = 0;
1064
- jobParams.nbWorkers = 0;
1065
- jobParams.jobSize = 0;
1066
- jobParams.overlapLog = 0;
1067
- jobParams.rsyncable = 0;
1068
- memset(&jobParams.ldmParams, 0, sizeof(ldmParams_t));
1069
- memset(&jobParams.customMem, 0, sizeof(ZSTD_customMem));
1070
- return jobParams;
1071
- }
1072
-
1073
1016
 
1074
1017
  /* ZSTDMT_resize() :
1075
1018
  * @return : error code if fails, 0 on success */
@@ -1098,7 +1041,7 @@ void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx* mtctx, const ZSTD_CCtx_p
1098
1041
  DEBUGLOG(5, "ZSTDMT_updateCParams_whileCompressing (level:%i)",
1099
1042
  compressionLevel);
1100
1043
  mtctx->params.compressionLevel = compressionLevel;
1101
- { ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, 0);
1044
+ { ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, 0, ZSTD_cpm_noAttachDict);
1102
1045
  cParams.windowLog = saved_wlog;
1103
1046
  mtctx->params.cParams = cParams;
1104
1047
  }
@@ -1185,8 +1128,8 @@ static unsigned ZSTDMT_computeTargetJobLog(const ZSTD_CCtx_params* params)
1185
1128
  if (params->ldmParams.enableLdm) {
1186
1129
  /* In Long Range Mode, the windowLog is typically oversized.
1187
1130
  * In which case, it's preferable to determine the jobSize
1188
- * based on chainLog instead. */
1189
- jobLog = MAX(21, params->cParams.chainLog + 4);
1131
+ * based on cycleLog instead. */
1132
+ jobLog = MAX(21, ZSTD_cycleLog(params->cParams.chainLog, params->cParams.strategy) + 3);
1190
1133
  } else {
1191
1134
  jobLog = MAX(20, params->cParams.windowLog + 2);
1192
1135
  }
@@ -1240,174 +1183,6 @@ static size_t ZSTDMT_computeOverlapSize(const ZSTD_CCtx_params* params)
1240
1183
  return (ovLog==0) ? 0 : (size_t)1 << ovLog;
1241
1184
  }
1242
1185
 
1243
- static unsigned
1244
- ZSTDMT_computeNbJobs(const ZSTD_CCtx_params* params, size_t srcSize, unsigned nbWorkers)
1245
- {
1246
- assert(nbWorkers>0);
1247
- { size_t const jobSizeTarget = (size_t)1 << ZSTDMT_computeTargetJobLog(params);
1248
- size_t const jobMaxSize = jobSizeTarget << 2;
1249
- size_t const passSizeMax = jobMaxSize * nbWorkers;
1250
- unsigned const multiplier = (unsigned)(srcSize / passSizeMax) + 1;
1251
- unsigned const nbJobsLarge = multiplier * nbWorkers;
1252
- unsigned const nbJobsMax = (unsigned)(srcSize / jobSizeTarget) + 1;
1253
- unsigned const nbJobsSmall = MIN(nbJobsMax, nbWorkers);
1254
- return (multiplier>1) ? nbJobsLarge : nbJobsSmall;
1255
- } }
1256
-
1257
- /* ZSTDMT_compress_advanced_internal() :
1258
- * This is a blocking function : it will only give back control to caller after finishing its compression job.
1259
- */
1260
- static size_t
1261
- ZSTDMT_compress_advanced_internal(
1262
- ZSTDMT_CCtx* mtctx,
1263
- void* dst, size_t dstCapacity,
1264
- const void* src, size_t srcSize,
1265
- const ZSTD_CDict* cdict,
1266
- ZSTD_CCtx_params params)
1267
- {
1268
- ZSTD_CCtx_params const jobParams = ZSTDMT_initJobCCtxParams(&params);
1269
- size_t const overlapSize = ZSTDMT_computeOverlapSize(&params);
1270
- unsigned const nbJobs = ZSTDMT_computeNbJobs(&params, srcSize, params.nbWorkers);
1271
- size_t const proposedJobSize = (srcSize + (nbJobs-1)) / nbJobs;
1272
- size_t const avgJobSize = (((proposedJobSize-1) & 0x1FFFF) < 0x7FFF) ? proposedJobSize + 0xFFFF : proposedJobSize; /* avoid too small last block */
1273
- const char* const srcStart = (const char*)src;
1274
- size_t remainingSrcSize = srcSize;
1275
- unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbJobs : (unsigned)(dstCapacity / ZSTD_compressBound(avgJobSize)); /* presumes avgJobSize >= 256 KB, which should be the case */
1276
- size_t frameStartPos = 0, dstBufferPos = 0;
1277
- assert(jobParams.nbWorkers == 0);
1278
- assert(mtctx->cctxPool->totalCCtx == params.nbWorkers);
1279
-
1280
- params.jobSize = (U32)avgJobSize;
1281
- DEBUGLOG(4, "ZSTDMT_compress_advanced_internal: nbJobs=%2u (rawSize=%u bytes; fixedSize=%u) ",
1282
- nbJobs, (U32)proposedJobSize, (U32)avgJobSize);
1283
-
1284
- if ((nbJobs==1) | (params.nbWorkers<=1)) { /* fallback to single-thread mode : this is a blocking invocation anyway */
1285
- ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0];
1286
- DEBUGLOG(4, "ZSTDMT_compress_advanced_internal: fallback to single-thread mode");
1287
- if (cdict) return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, jobParams.fParams);
1288
- return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, NULL, 0, &jobParams);
1289
- }
1290
-
1291
- 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) */
1292
- ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(avgJobSize) );
1293
- /* LDM doesn't even try to load the dictionary in single-ingestion mode */
1294
- if (ZSTDMT_serialState_reset(&mtctx->serial, mtctx->seqPool, params, avgJobSize, NULL, 0, ZSTD_dct_auto))
1295
- return ERROR(memory_allocation);
1296
-
1297
- FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbJobs) , ""); /* only expands if necessary */
1298
-
1299
- { unsigned u;
1300
- for (u=0; u<nbJobs; u++) {
1301
- size_t const jobSize = MIN(remainingSrcSize, avgJobSize);
1302
- size_t const dstBufferCapacity = ZSTD_compressBound(jobSize);
1303
- buffer_t const dstAsBuffer = { (char*)dst + dstBufferPos, dstBufferCapacity };
1304
- buffer_t const dstBuffer = u < compressWithinDst ? dstAsBuffer : g_nullBuffer;
1305
- size_t dictSize = u ? overlapSize : 0;
1306
-
1307
- mtctx->jobs[u].prefix.start = srcStart + frameStartPos - dictSize;
1308
- mtctx->jobs[u].prefix.size = dictSize;
1309
- mtctx->jobs[u].src.start = srcStart + frameStartPos;
1310
- mtctx->jobs[u].src.size = jobSize; assert(jobSize > 0); /* avoid job.src.size == 0 */
1311
- mtctx->jobs[u].consumed = 0;
1312
- mtctx->jobs[u].cSize = 0;
1313
- mtctx->jobs[u].cdict = (u==0) ? cdict : NULL;
1314
- mtctx->jobs[u].fullFrameSize = srcSize;
1315
- mtctx->jobs[u].params = jobParams;
1316
- /* do not calculate checksum within sections, but write it in header for first section */
1317
- mtctx->jobs[u].dstBuff = dstBuffer;
1318
- mtctx->jobs[u].cctxPool = mtctx->cctxPool;
1319
- mtctx->jobs[u].bufPool = mtctx->bufPool;
1320
- mtctx->jobs[u].seqPool = mtctx->seqPool;
1321
- mtctx->jobs[u].serial = &mtctx->serial;
1322
- mtctx->jobs[u].jobID = u;
1323
- mtctx->jobs[u].firstJob = (u==0);
1324
- mtctx->jobs[u].lastJob = (u==nbJobs-1);
1325
-
1326
- DEBUGLOG(5, "ZSTDMT_compress_advanced_internal: posting job %u (%u bytes)", u, (U32)jobSize);
1327
- DEBUG_PRINTHEX(6, mtctx->jobs[u].prefix.start, 12);
1328
- POOL_add(mtctx->factory, ZSTDMT_compressionJob, &mtctx->jobs[u]);
1329
-
1330
- frameStartPos += jobSize;
1331
- dstBufferPos += dstBufferCapacity;
1332
- remainingSrcSize -= jobSize;
1333
- } }
1334
-
1335
- /* collect result */
1336
- { size_t error = 0, dstPos = 0;
1337
- unsigned jobID;
1338
- for (jobID=0; jobID<nbJobs; jobID++) {
1339
- DEBUGLOG(5, "waiting for job %u ", jobID);
1340
- ZSTD_PTHREAD_MUTEX_LOCK(&mtctx->jobs[jobID].job_mutex);
1341
- while (mtctx->jobs[jobID].consumed < mtctx->jobs[jobID].src.size) {
1342
- DEBUGLOG(5, "waiting for jobCompleted signal from job %u", jobID);
1343
- ZSTD_pthread_cond_wait(&mtctx->jobs[jobID].job_cond, &mtctx->jobs[jobID].job_mutex);
1344
- }
1345
- ZSTD_pthread_mutex_unlock(&mtctx->jobs[jobID].job_mutex);
1346
- DEBUGLOG(5, "ready to write job %u ", jobID);
1347
-
1348
- { size_t const cSize = mtctx->jobs[jobID].cSize;
1349
- if (ZSTD_isError(cSize)) error = cSize;
1350
- if ((!error) && (dstPos + cSize > dstCapacity)) error = ERROR(dstSize_tooSmall);
1351
- if (jobID) { /* note : job 0 is written directly at dst, which is correct position */
1352
- if (!error)
1353
- memmove((char*)dst + dstPos, mtctx->jobs[jobID].dstBuff.start, cSize); /* may overlap when job compressed within dst */
1354
- if (jobID >= compressWithinDst) { /* job compressed into its own buffer, which must be released */
1355
- DEBUGLOG(5, "releasing buffer %u>=%u", jobID, compressWithinDst);
1356
- ZSTDMT_releaseBuffer(mtctx->bufPool, mtctx->jobs[jobID].dstBuff);
1357
- } }
1358
- mtctx->jobs[jobID].dstBuff = g_nullBuffer;
1359
- mtctx->jobs[jobID].cSize = 0;
1360
- dstPos += cSize ;
1361
- }
1362
- } /* for (jobID=0; jobID<nbJobs; jobID++) */
1363
-
1364
- DEBUGLOG(4, "checksumFlag : %u ", params.fParams.checksumFlag);
1365
- if (params.fParams.checksumFlag) {
1366
- U32 const checksum = (U32)XXH64_digest(&mtctx->serial.xxhState);
1367
- if (dstPos + 4 > dstCapacity) {
1368
- error = ERROR(dstSize_tooSmall);
1369
- } else {
1370
- DEBUGLOG(4, "writing checksum : %08X \n", checksum);
1371
- MEM_writeLE32((char*)dst + dstPos, checksum);
1372
- dstPos += 4;
1373
- } }
1374
-
1375
- if (!error) DEBUGLOG(4, "compressed size : %u ", (U32)dstPos);
1376
- return error ? error : dstPos;
1377
- }
1378
- }
1379
-
1380
- size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx,
1381
- void* dst, size_t dstCapacity,
1382
- const void* src, size_t srcSize,
1383
- const ZSTD_CDict* cdict,
1384
- ZSTD_parameters params,
1385
- int overlapLog)
1386
- {
1387
- ZSTD_CCtx_params cctxParams = mtctx->params;
1388
- cctxParams.cParams = params.cParams;
1389
- cctxParams.fParams = params.fParams;
1390
- assert(ZSTD_OVERLAPLOG_MIN <= overlapLog && overlapLog <= ZSTD_OVERLAPLOG_MAX);
1391
- cctxParams.overlapLog = overlapLog;
1392
- return ZSTDMT_compress_advanced_internal(mtctx,
1393
- dst, dstCapacity,
1394
- src, srcSize,
1395
- cdict, cctxParams);
1396
- }
1397
-
1398
-
1399
- size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
1400
- void* dst, size_t dstCapacity,
1401
- const void* src, size_t srcSize,
1402
- int compressionLevel)
1403
- {
1404
- ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, 0);
1405
- int const overlapLog = ZSTDMT_overlapLog_default(params.cParams.strategy);
1406
- params.fParams.contentSizeFlag = 1;
1407
- return ZSTDMT_compress_advanced(mtctx, dst, dstCapacity, src, srcSize, NULL, params, overlapLog);
1408
- }
1409
-
1410
-
1411
1186
  /* ====================================== */
1412
1187
  /* ======= Streaming API ======= */
1413
1188
  /* ====================================== */
@@ -1432,16 +1207,6 @@ size_t ZSTDMT_initCStream_internal(
1432
1207
  if (params.jobSize != 0 && params.jobSize < ZSTDMT_JOBSIZE_MIN) params.jobSize = ZSTDMT_JOBSIZE_MIN;
1433
1208
  if (params.jobSize > (size_t)ZSTDMT_JOBSIZE_MAX) params.jobSize = (size_t)ZSTDMT_JOBSIZE_MAX;
1434
1209
 
1435
- mtctx->singleBlockingThread = (pledgedSrcSize <= ZSTDMT_JOBSIZE_MIN); /* do not trigger multi-threading when srcSize is too small */
1436
- if (mtctx->singleBlockingThread) {
1437
- ZSTD_CCtx_params const singleThreadParams = ZSTDMT_initJobCCtxParams(&params);
1438
- DEBUGLOG(5, "ZSTDMT_initCStream_internal: switch to single blocking thread mode");
1439
- assert(singleThreadParams.nbWorkers == 0);
1440
- return ZSTD_initCStream_internal(mtctx->cctxPool->cctx[0],
1441
- dict, dictSize, cdict,
1442
- &singleThreadParams, pledgedSrcSize);
1443
- }
1444
-
1445
1210
  DEBUGLOG(4, "ZSTDMT_initCStream_internal: %u workers", params.nbWorkers);
1446
1211
 
1447
1212
  if (mtctx->allJobsCompleted == 0) { /* previous compression not correctly finished */
@@ -1504,8 +1269,8 @@ size_t ZSTDMT_initCStream_internal(
1504
1269
  size_t const capacity = MAX(windowSize, sectionsSize) + slackSize;
1505
1270
  if (mtctx->roundBuff.capacity < capacity) {
1506
1271
  if (mtctx->roundBuff.buffer)
1507
- ZSTD_free(mtctx->roundBuff.buffer, mtctx->cMem);
1508
- mtctx->roundBuff.buffer = (BYTE*)ZSTD_malloc(capacity, mtctx->cMem);
1272
+ ZSTD_customFree(mtctx->roundBuff.buffer, mtctx->cMem);
1273
+ mtctx->roundBuff.buffer = (BYTE*)ZSTD_customMalloc(capacity, mtctx->cMem);
1509
1274
  if (mtctx->roundBuff.buffer == NULL) {
1510
1275
  mtctx->roundBuff.capacity = 0;
1511
1276
  return ERROR(memory_allocation);
@@ -1530,53 +1295,6 @@ size_t ZSTDMT_initCStream_internal(
1530
1295
  return 0;
1531
1296
  }
1532
1297
 
1533
- size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx,
1534
- const void* dict, size_t dictSize,
1535
- ZSTD_parameters params,
1536
- unsigned long long pledgedSrcSize)
1537
- {
1538
- ZSTD_CCtx_params cctxParams = mtctx->params; /* retrieve sticky params */
1539
- DEBUGLOG(4, "ZSTDMT_initCStream_advanced (pledgedSrcSize=%u)", (U32)pledgedSrcSize);
1540
- cctxParams.cParams = params.cParams;
1541
- cctxParams.fParams = params.fParams;
1542
- return ZSTDMT_initCStream_internal(mtctx, dict, dictSize, ZSTD_dct_auto, NULL,
1543
- cctxParams, pledgedSrcSize);
1544
- }
1545
-
1546
- size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx,
1547
- const ZSTD_CDict* cdict,
1548
- ZSTD_frameParameters fParams,
1549
- unsigned long long pledgedSrcSize)
1550
- {
1551
- ZSTD_CCtx_params cctxParams = mtctx->params;
1552
- if (cdict==NULL) return ERROR(dictionary_wrong); /* method incompatible with NULL cdict */
1553
- cctxParams.cParams = ZSTD_getCParamsFromCDict(cdict);
1554
- cctxParams.fParams = fParams;
1555
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0 /*dictSize*/, ZSTD_dct_auto, cdict,
1556
- cctxParams, pledgedSrcSize);
1557
- }
1558
-
1559
-
1560
- /* ZSTDMT_resetCStream() :
1561
- * pledgedSrcSize can be zero == unknown (for the time being)
1562
- * prefer using ZSTD_CONTENTSIZE_UNKNOWN,
1563
- * as `0` might mean "empty" in the future */
1564
- size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* mtctx, unsigned long long pledgedSrcSize)
1565
- {
1566
- if (!pledgedSrcSize) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN;
1567
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0, ZSTD_dct_auto, 0, mtctx->params,
1568
- pledgedSrcSize);
1569
- }
1570
-
1571
- size_t ZSTDMT_initCStream(ZSTDMT_CCtx* mtctx, int compressionLevel) {
1572
- ZSTD_parameters const params = ZSTD_getParams(compressionLevel, ZSTD_CONTENTSIZE_UNKNOWN, 0);
1573
- ZSTD_CCtx_params cctxParams = mtctx->params; /* retrieve sticky params */
1574
- DEBUGLOG(4, "ZSTDMT_initCStream (cLevel=%i)", compressionLevel);
1575
- cctxParams.cParams = params.cParams;
1576
- cctxParams.fParams = params.fParams;
1577
- return ZSTDMT_initCStream_internal(mtctx, NULL, 0, ZSTD_dct_auto, NULL, cctxParams, ZSTD_CONTENTSIZE_UNKNOWN);
1578
- }
1579
-
1580
1298
 
1581
1299
  /* ZSTDMT_writeLastEmptyBlock()
1582
1300
  * Write a single empty block with an end-of-frame to finish a frame.
@@ -1740,7 +1458,7 @@ static size_t ZSTDMT_flushProduced(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, u
1740
1458
  assert(cSize >= mtctx->jobs[wJobID].dstFlushed);
1741
1459
  assert(mtctx->jobs[wJobID].dstBuff.start != NULL);
1742
1460
  if (toFlush > 0) {
1743
- memcpy((char*)output->dst + output->pos,
1461
+ ZSTD_memcpy((char*)output->dst + output->pos,
1744
1462
  (const char*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].dstFlushed,
1745
1463
  toFlush);
1746
1464
  }
@@ -1894,7 +1612,7 @@ static int ZSTDMT_tryGetInputRange(ZSTDMT_CCtx* mtctx)
1894
1612
  return 0;
1895
1613
  }
1896
1614
  ZSTDMT_waitForLdmComplete(mtctx, buffer);
1897
- memmove(start, mtctx->inBuff.prefix.start, prefixSize);
1615
+ ZSTD_memmove(start, mtctx->inBuff.prefix.start, prefixSize);
1898
1616
  mtctx->inBuff.prefix.start = start;
1899
1617
  mtctx->roundBuff.pos = prefixSize;
1900
1618
  }
@@ -1968,6 +1686,16 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1968
1686
  pos = 0;
1969
1687
  prev = (BYTE const*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled - RSYNC_LENGTH;
1970
1688
  hash = ZSTD_rollingHash_compute(prev, RSYNC_LENGTH);
1689
+ if ((hash & hitMask) == hitMask) {
1690
+ /* We're already at a sync point so don't load any more until
1691
+ * we're able to flush this sync point.
1692
+ * This likely happened because the job table was full so we
1693
+ * couldn't add our job.
1694
+ */
1695
+ syncPoint.toLoad = 0;
1696
+ syncPoint.flush = 1;
1697
+ return syncPoint;
1698
+ }
1971
1699
  } else {
1972
1700
  /* We don't have enough bytes buffered to initialize the hash, but
1973
1701
  * we know we have at least RSYNC_LENGTH bytes total.
@@ -2022,34 +1750,11 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2022
1750
  assert(output->pos <= output->size);
2023
1751
  assert(input->pos <= input->size);
2024
1752
 
2025
- if (mtctx->singleBlockingThread) { /* delegate to single-thread (synchronous) */
2026
- return ZSTD_compressStream2(mtctx->cctxPool->cctx[0], output, input, endOp);
2027
- }
2028
-
2029
1753
  if ((mtctx->frameEnded) && (endOp==ZSTD_e_continue)) {
2030
1754
  /* current frame being ended. Only flush/end are allowed */
2031
1755
  return ERROR(stage_wrong);
2032
1756
  }
2033
1757
 
2034
- /* single-pass shortcut (note : synchronous-mode) */
2035
- if ( (!mtctx->params.rsyncable) /* rsyncable mode is disabled */
2036
- && (mtctx->nextJobID == 0) /* just started */
2037
- && (mtctx->inBuff.filled == 0) /* nothing buffered */
2038
- && (!mtctx->jobReady) /* no job already created */
2039
- && (endOp == ZSTD_e_end) /* end order */
2040
- && (output->size - output->pos >= ZSTD_compressBound(input->size - input->pos)) ) { /* enough space in dst */
2041
- size_t const cSize = ZSTDMT_compress_advanced_internal(mtctx,
2042
- (char*)output->dst + output->pos, output->size - output->pos,
2043
- (const char*)input->src + input->pos, input->size - input->pos,
2044
- mtctx->cdict, mtctx->params);
2045
- if (ZSTD_isError(cSize)) return cSize;
2046
- input->pos = input->size;
2047
- output->pos += cSize;
2048
- mtctx->allJobsCompleted = 1;
2049
- mtctx->frameEnded = 1;
2050
- return 0;
2051
- }
2052
-
2053
1758
  /* fill input buffer */
2054
1759
  if ( (!mtctx->jobReady)
2055
1760
  && (input->size > input->pos) ) { /* support NULL input */
@@ -2072,13 +1777,21 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2072
1777
  assert(mtctx->inBuff.buffer.capacity >= mtctx->targetSectionSize);
2073
1778
  DEBUGLOG(5, "ZSTDMT_compressStream_generic: adding %u bytes on top of %u to buffer of size %u",
2074
1779
  (U32)syncPoint.toLoad, (U32)mtctx->inBuff.filled, (U32)mtctx->targetSectionSize);
2075
- memcpy((char*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, (const char*)input->src + input->pos, syncPoint.toLoad);
1780
+ ZSTD_memcpy((char*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, (const char*)input->src + input->pos, syncPoint.toLoad);
2076
1781
  input->pos += syncPoint.toLoad;
2077
1782
  mtctx->inBuff.filled += syncPoint.toLoad;
2078
1783
  forwardInputProgress = syncPoint.toLoad>0;
2079
1784
  }
2080
- if ((input->pos < input->size) && (endOp == ZSTD_e_end))
2081
- endOp = ZSTD_e_flush; /* can't end now : not all input consumed */
1785
+ }
1786
+ if ((input->pos < input->size) && (endOp == ZSTD_e_end)) {
1787
+ /* Can't end yet because the input is not fully consumed.
1788
+ * We are in one of these cases:
1789
+ * - mtctx->inBuff is NULL & empty: we couldn't get an input buffer so don't create a new job.
1790
+ * - We filled the input buffer: flush this job but don't end the frame.
1791
+ * - We hit a synchronization point: flush this job but don't end the frame.
1792
+ */
1793
+ assert(mtctx->inBuff.filled == 0 || mtctx->inBuff.filled == mtctx->targetSectionSize || mtctx->params.rsyncable);
1794
+ endOp = ZSTD_e_flush;
2082
1795
  }
2083
1796
 
2084
1797
  if ( (mtctx->jobReady)
@@ -2097,47 +1810,3 @@ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
2097
1810
  return remainingToFlush;
2098
1811
  }
2099
1812
  }
2100
-
2101
-
2102
- size_t ZSTDMT_compressStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
2103
- {
2104
- FORWARD_IF_ERROR( ZSTDMT_compressStream_generic(mtctx, output, input, ZSTD_e_continue) , "");
2105
-
2106
- /* recommended next input size : fill current input buffer */
2107
- return mtctx->targetSectionSize - mtctx->inBuff.filled; /* note : could be zero when input buffer is fully filled and no more availability to create new job */
2108
- }
2109
-
2110
-
2111
- static size_t ZSTDMT_flushStream_internal(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_EndDirective endFrame)
2112
- {
2113
- size_t const srcSize = mtctx->inBuff.filled;
2114
- DEBUGLOG(5, "ZSTDMT_flushStream_internal");
2115
-
2116
- if ( mtctx->jobReady /* one job ready for a worker to pick up */
2117
- || (srcSize > 0) /* still some data within input buffer */
2118
- || ((endFrame==ZSTD_e_end) && !mtctx->frameEnded)) { /* need a last 0-size block to end frame */
2119
- DEBUGLOG(5, "ZSTDMT_flushStream_internal : create a new job (%u bytes, end:%u)",
2120
- (U32)srcSize, (U32)endFrame);
2121
- FORWARD_IF_ERROR( ZSTDMT_createCompressionJob(mtctx, srcSize, endFrame) , "");
2122
- }
2123
-
2124
- /* check if there is any data available to flush */
2125
- return ZSTDMT_flushProduced(mtctx, output, 1 /* blockToFlush */, endFrame);
2126
- }
2127
-
2128
-
2129
- size_t ZSTDMT_flushStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output)
2130
- {
2131
- DEBUGLOG(5, "ZSTDMT_flushStream");
2132
- if (mtctx->singleBlockingThread)
2133
- return ZSTD_flushStream(mtctx->cctxPool->cctx[0], output);
2134
- return ZSTDMT_flushStream_internal(mtctx, output, ZSTD_e_flush);
2135
- }
2136
-
2137
- size_t ZSTDMT_endStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output)
2138
- {
2139
- DEBUGLOG(4, "ZSTDMT_endStream");
2140
- if (mtctx->singleBlockingThread)
2141
- return ZSTD_endStream(mtctx->cctxPool->cctx[0], output);
2142
- return ZSTDMT_flushStream_internal(mtctx, output, ZSTD_e_end);
2143
- }