extzstd 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja +5 -0
  3. data/README.md +5 -5
  4. data/contrib/zstd/CONTRIBUTING.md +42 -0
  5. data/contrib/zstd/LICENSE-examples +11 -0
  6. data/contrib/zstd/Makefile +315 -0
  7. data/contrib/zstd/NEWS +261 -0
  8. data/contrib/zstd/PATENTS +33 -0
  9. data/contrib/zstd/README.md +121 -41
  10. data/contrib/zstd/TESTING.md +44 -0
  11. data/contrib/zstd/appveyor.yml +178 -0
  12. data/contrib/zstd/circle.yml +75 -0
  13. data/contrib/zstd/lib/BUCK +186 -0
  14. data/contrib/zstd/lib/Makefile +163 -0
  15. data/contrib/zstd/lib/README.md +77 -0
  16. data/contrib/zstd/{common → lib/common}/bitstream.h +7 -4
  17. data/contrib/zstd/{common → lib/common}/entropy_common.c +19 -23
  18. data/contrib/zstd/{common → lib/common}/error_private.c +0 -0
  19. data/contrib/zstd/{common → lib/common}/error_private.h +0 -0
  20. data/contrib/zstd/{common → lib/common}/fse.h +94 -34
  21. data/contrib/zstd/{common → lib/common}/fse_decompress.c +18 -19
  22. data/contrib/zstd/{common → lib/common}/huf.h +52 -20
  23. data/contrib/zstd/{common → lib/common}/mem.h +17 -13
  24. data/contrib/zstd/lib/common/pool.c +194 -0
  25. data/contrib/zstd/lib/common/pool.h +56 -0
  26. data/contrib/zstd/lib/common/threading.c +80 -0
  27. data/contrib/zstd/lib/common/threading.h +104 -0
  28. data/contrib/zstd/{common → lib/common}/xxhash.c +3 -1
  29. data/contrib/zstd/{common → lib/common}/xxhash.h +11 -15
  30. data/contrib/zstd/{common → lib/common}/zstd_common.c +1 -11
  31. data/contrib/zstd/{common → lib/common}/zstd_errors.h +16 -2
  32. data/contrib/zstd/{common → lib/common}/zstd_internal.h +17 -1
  33. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +138 -91
  34. data/contrib/zstd/{compress → lib/compress}/huf_compress.c +218 -67
  35. data/contrib/zstd/{compress → lib/compress}/zstd_compress.c +231 -108
  36. data/contrib/zstd/{compress → lib/compress}/zstd_opt.h +44 -25
  37. data/contrib/zstd/lib/compress/zstdmt_compress.c +739 -0
  38. data/contrib/zstd/lib/compress/zstdmt_compress.h +78 -0
  39. data/contrib/zstd/{decompress → lib/decompress}/huf_decompress.c +28 -23
  40. data/contrib/zstd/{decompress → lib/decompress}/zstd_decompress.c +814 -176
  41. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +60 -39
  42. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  43. data/contrib/zstd/lib/deprecated/zbuff_compress.c +145 -0
  44. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +74 -0
  45. data/contrib/zstd/lib/dictBuilder/cover.c +1029 -0
  46. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +0 -0
  47. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
  48. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +68 -18
  49. data/contrib/zstd/lib/dictBuilder/zdict.h +201 -0
  50. data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +122 -7
  51. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +34 -3
  52. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +8 -0
  53. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +45 -12
  54. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +8 -0
  55. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +45 -12
  56. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +8 -0
  57. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +56 -33
  58. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +8 -0
  59. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +45 -18
  60. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +7 -0
  61. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +43 -16
  62. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +7 -0
  63. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +57 -23
  64. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +8 -0
  65. data/contrib/zstd/lib/libzstd.pc.in +14 -0
  66. data/contrib/zstd/{zstd.h → lib/zstd.h} +206 -71
  67. data/ext/depend +2 -0
  68. data/ext/extconf.rb +4 -4
  69. data/ext/extzstd.c +1 -1
  70. data/ext/zstd_common.c +5 -5
  71. data/ext/zstd_compress.c +3 -3
  72. data/ext/zstd_decompress.c +2 -2
  73. data/ext/zstd_dictbuilder.c +2 -2
  74. data/ext/zstd_legacy_v01.c +1 -1
  75. data/ext/zstd_legacy_v02.c +1 -1
  76. data/ext/zstd_legacy_v03.c +1 -1
  77. data/ext/zstd_legacy_v04.c +1 -1
  78. data/ext/zstd_legacy_v05.c +1 -1
  79. data/ext/zstd_legacy_v06.c +1 -1
  80. data/ext/zstd_legacy_v07.c +1 -1
  81. data/gemstub.rb +9 -5
  82. data/lib/extzstd/version.rb +1 -1
  83. metadata +73 -51
  84. data/contrib/zstd/compress/zbuff_compress.c +0 -319
  85. data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
  86. data/contrib/zstd/dictBuilder/zdict.h +0 -111
@@ -0,0 +1,739 @@
1
+ /**
2
+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+
11
+ /* ====== Tuning parameters ====== */
12
+ #define ZSTDMT_NBTHREADS_MAX 128
13
+
14
+
15
+ /* ====== Compiler specifics ====== */
16
+ #if defined(_MSC_VER)
17
+ # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
18
+ #endif
19
+
20
+
21
+ /* ====== Dependencies ====== */
22
+ #include <stdlib.h> /* malloc */
23
+ #include <string.h> /* memcpy */
24
+ #include "pool.h" /* threadpool */
25
+ #include "threading.h" /* mutex */
26
+ #include "zstd_internal.h" /* MIN, ERROR, ZSTD_*, ZSTD_highbit32 */
27
+ #include "zstdmt_compress.h"
28
+
29
+
30
+ /* ====== Debug ====== */
31
+ #if 0
32
+
33
+ # include <stdio.h>
34
+ # include <unistd.h>
35
+ # include <sys/times.h>
36
+ static unsigned g_debugLevel = 3;
37
+ # define DEBUGLOGRAW(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __VA_ARGS__); }
38
+ # define DEBUGLOG(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __FILE__ ": "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, " \n"); }
39
+
40
+ # define DEBUG_PRINTHEX(l,p,n) { \
41
+ unsigned debug_u; \
42
+ for (debug_u=0; debug_u<(n); debug_u++) \
43
+ DEBUGLOGRAW(l, "%02X ", ((const unsigned char*)(p))[debug_u]); \
44
+ DEBUGLOGRAW(l, " \n"); \
45
+ }
46
+
47
+ static unsigned long long GetCurrentClockTimeMicroseconds()
48
+ {
49
+ static clock_t _ticksPerSecond = 0;
50
+ if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK);
51
+
52
+ struct tms junk; clock_t newTicks = (clock_t) times(&junk);
53
+ return ((((unsigned long long)newTicks)*(1000000))/_ticksPerSecond);
54
+ }
55
+
56
+ #define MUTEX_WAIT_TIME_DLEVEL 5
57
+ #define PTHREAD_MUTEX_LOCK(mutex) \
58
+ if (g_debugLevel>=MUTEX_WAIT_TIME_DLEVEL) { \
59
+ unsigned long long beforeTime = GetCurrentClockTimeMicroseconds(); \
60
+ pthread_mutex_lock(mutex); \
61
+ unsigned long long afterTime = GetCurrentClockTimeMicroseconds(); \
62
+ unsigned long long elapsedTime = (afterTime-beforeTime); \
63
+ if (elapsedTime > 1000) { /* or whatever threshold you like; I'm using 1 millisecond here */ \
64
+ DEBUGLOG(MUTEX_WAIT_TIME_DLEVEL, "Thread took %llu microseconds to acquire mutex %s \n", \
65
+ elapsedTime, #mutex); \
66
+ } \
67
+ } else pthread_mutex_lock(mutex);
68
+
69
+ #else
70
+
71
+ # define DEBUGLOG(l, ...) {} /* disabled */
72
+ # define PTHREAD_MUTEX_LOCK(m) pthread_mutex_lock(m)
73
+ # define DEBUG_PRINTHEX(l,p,n) {}
74
+
75
+ #endif
76
+
77
+
78
+ /* ===== Buffer Pool ===== */
79
+
80
+ typedef struct buffer_s {
81
+ void* start;
82
+ size_t size;
83
+ } buffer_t;
84
+
85
+ static const buffer_t g_nullBuffer = { NULL, 0 };
86
+
87
+ typedef struct ZSTDMT_bufferPool_s {
88
+ unsigned totalBuffers;
89
+ unsigned nbBuffers;
90
+ buffer_t bTable[1]; /* variable size */
91
+ } ZSTDMT_bufferPool;
92
+
93
+ static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned nbThreads)
94
+ {
95
+ unsigned const maxNbBuffers = 2*nbThreads + 2;
96
+ ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)calloc(1, sizeof(ZSTDMT_bufferPool) + (maxNbBuffers-1) * sizeof(buffer_t));
97
+ if (bufPool==NULL) return NULL;
98
+ bufPool->totalBuffers = maxNbBuffers;
99
+ bufPool->nbBuffers = 0;
100
+ return bufPool;
101
+ }
102
+
103
+ static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool* bufPool)
104
+ {
105
+ unsigned u;
106
+ if (!bufPool) return; /* compatibility with free on NULL */
107
+ for (u=0; u<bufPool->totalBuffers; u++)
108
+ free(bufPool->bTable[u].start);
109
+ free(bufPool);
110
+ }
111
+
112
+ /* assumption : invocation from main thread only ! */
113
+ static buffer_t ZSTDMT_getBuffer(ZSTDMT_bufferPool* pool, size_t bSize)
114
+ {
115
+ if (pool->nbBuffers) { /* try to use an existing buffer */
116
+ buffer_t const buf = pool->bTable[--(pool->nbBuffers)];
117
+ size_t const availBufferSize = buf.size;
118
+ if ((availBufferSize >= bSize) & (availBufferSize <= 10*bSize)) /* large enough, but not too much */
119
+ return buf;
120
+ free(buf.start); /* size conditions not respected : scratch this buffer and create a new one */
121
+ }
122
+ /* create new buffer */
123
+ { buffer_t buffer;
124
+ void* const start = malloc(bSize);
125
+ if (start==NULL) bSize = 0;
126
+ buffer.start = start; /* note : start can be NULL if malloc fails ! */
127
+ buffer.size = bSize;
128
+ return buffer;
129
+ }
130
+ }
131
+
132
+ /* store buffer for later re-use, up to pool capacity */
133
+ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* pool, buffer_t buf)
134
+ {
135
+ if (buf.start == NULL) return; /* release on NULL */
136
+ if (pool->nbBuffers < pool->totalBuffers) {
137
+ pool->bTable[pool->nbBuffers++] = buf; /* store for later re-use */
138
+ return;
139
+ }
140
+ /* Reached bufferPool capacity (should not happen) */
141
+ free(buf.start);
142
+ }
143
+
144
+
145
+ /* ===== CCtx Pool ===== */
146
+
147
+ typedef struct {
148
+ unsigned totalCCtx;
149
+ unsigned availCCtx;
150
+ ZSTD_CCtx* cctx[1]; /* variable size */
151
+ } ZSTDMT_CCtxPool;
152
+
153
+ /* assumption : CCtxPool invocation only from main thread */
154
+
155
+ /* note : all CCtx borrowed from the pool should be released back to the pool _before_ freeing the pool */
156
+ static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
157
+ {
158
+ unsigned u;
159
+ for (u=0; u<pool->totalCCtx; u++)
160
+ ZSTD_freeCCtx(pool->cctx[u]); /* note : compatible with free on NULL */
161
+ free(pool);
162
+ }
163
+
164
+ /* ZSTDMT_createCCtxPool() :
165
+ * implies nbThreads >= 1 , checked by caller ZSTDMT_createCCtx() */
166
+ static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(unsigned nbThreads)
167
+ {
168
+ ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) calloc(1, sizeof(ZSTDMT_CCtxPool) + (nbThreads-1)*sizeof(ZSTD_CCtx*));
169
+ if (!cctxPool) return NULL;
170
+ cctxPool->totalCCtx = nbThreads;
171
+ cctxPool->availCCtx = 1; /* at least one cctx for single-thread mode */
172
+ cctxPool->cctx[0] = ZSTD_createCCtx();
173
+ if (!cctxPool->cctx[0]) { ZSTDMT_freeCCtxPool(cctxPool); return NULL; }
174
+ DEBUGLOG(1, "cctxPool created, with %u threads", nbThreads);
175
+ return cctxPool;
176
+ }
177
+
178
+ static ZSTD_CCtx* ZSTDMT_getCCtx(ZSTDMT_CCtxPool* pool)
179
+ {
180
+ if (pool->availCCtx) {
181
+ pool->availCCtx--;
182
+ return pool->cctx[pool->availCCtx];
183
+ }
184
+ return ZSTD_createCCtx(); /* note : can be NULL, when creation fails ! */
185
+ }
186
+
187
+ static void ZSTDMT_releaseCCtx(ZSTDMT_CCtxPool* pool, ZSTD_CCtx* cctx)
188
+ {
189
+ if (cctx==NULL) return; /* compatibility with release on NULL */
190
+ if (pool->availCCtx < pool->totalCCtx)
191
+ pool->cctx[pool->availCCtx++] = cctx;
192
+ else
193
+ /* pool overflow : should not happen, since totalCCtx==nbThreads */
194
+ ZSTD_freeCCtx(cctx);
195
+ }
196
+
197
+
198
+ /* ===== Thread worker ===== */
199
+
200
+ typedef struct {
201
+ buffer_t buffer;
202
+ size_t filled;
203
+ } inBuff_t;
204
+
205
+ typedef struct {
206
+ ZSTD_CCtx* cctx;
207
+ buffer_t src;
208
+ const void* srcStart;
209
+ size_t srcSize;
210
+ size_t dictSize;
211
+ buffer_t dstBuff;
212
+ size_t cSize;
213
+ size_t dstFlushed;
214
+ unsigned firstChunk;
215
+ unsigned lastChunk;
216
+ unsigned jobCompleted;
217
+ unsigned jobScanned;
218
+ pthread_mutex_t* jobCompleted_mutex;
219
+ pthread_cond_t* jobCompleted_cond;
220
+ ZSTD_parameters params;
221
+ ZSTD_CDict* cdict;
222
+ unsigned long long fullFrameSize;
223
+ } ZSTDMT_jobDescription;
224
+
225
+ /* ZSTDMT_compressChunk() : POOL_function type */
226
+ void ZSTDMT_compressChunk(void* jobDescription)
227
+ {
228
+ ZSTDMT_jobDescription* const job = (ZSTDMT_jobDescription*)jobDescription;
229
+ const void* const src = (const char*)job->srcStart + job->dictSize;
230
+ buffer_t const dstBuff = job->dstBuff;
231
+ DEBUGLOG(3, "job (first:%u) (last:%u) : dictSize %u, srcSize %u", job->firstChunk, job->lastChunk, (U32)job->dictSize, (U32)job->srcSize);
232
+ if (job->cdict) { /* should only happen for first segment */
233
+ size_t const initError = ZSTD_compressBegin_usingCDict(job->cctx, job->cdict, job->fullFrameSize);
234
+ if (job->cdict) DEBUGLOG(3, "using CDict ");
235
+ if (ZSTD_isError(initError)) { job->cSize = initError; goto _endJob; }
236
+ } else { /* srcStart points at reloaded section */
237
+ size_t const dictModeError = ZSTD_setCCtxParameter(job->cctx, ZSTD_p_forceRawDict, 1); /* Force loading dictionary in "content-only" mode (no header analysis) */
238
+ size_t const initError = ZSTD_compressBegin_advanced(job->cctx, job->srcStart, job->dictSize, job->params, 0);
239
+ if (ZSTD_isError(initError) || ZSTD_isError(dictModeError)) { job->cSize = initError; goto _endJob; }
240
+ ZSTD_setCCtxParameter(job->cctx, ZSTD_p_forceWindow, 1);
241
+ }
242
+ if (!job->firstChunk) { /* flush and overwrite frame header when it's not first segment */
243
+ size_t const hSize = ZSTD_compressContinue(job->cctx, dstBuff.start, dstBuff.size, src, 0);
244
+ if (ZSTD_isError(hSize)) { job->cSize = hSize; goto _endJob; }
245
+ ZSTD_invalidateRepCodes(job->cctx);
246
+ }
247
+
248
+ DEBUGLOG(4, "Compressing : ");
249
+ DEBUG_PRINTHEX(4, job->srcStart, 12);
250
+ job->cSize = (job->lastChunk) ?
251
+ ZSTD_compressEnd (job->cctx, dstBuff.start, dstBuff.size, src, job->srcSize) :
252
+ ZSTD_compressContinue(job->cctx, dstBuff.start, dstBuff.size, src, job->srcSize);
253
+ DEBUGLOG(3, "compressed %u bytes into %u bytes (first:%u) (last:%u)", (unsigned)job->srcSize, (unsigned)job->cSize, job->firstChunk, job->lastChunk);
254
+
255
+ _endJob:
256
+ PTHREAD_MUTEX_LOCK(job->jobCompleted_mutex);
257
+ job->jobCompleted = 1;
258
+ job->jobScanned = 0;
259
+ pthread_cond_signal(job->jobCompleted_cond);
260
+ pthread_mutex_unlock(job->jobCompleted_mutex);
261
+ }
262
+
263
+
264
+ /* ------------------------------------------ */
265
+ /* ===== Multi-threaded compression ===== */
266
+ /* ------------------------------------------ */
267
+
268
+ struct ZSTDMT_CCtx_s {
269
+ POOL_ctx* factory;
270
+ ZSTDMT_bufferPool* buffPool;
271
+ ZSTDMT_CCtxPool* cctxPool;
272
+ pthread_mutex_t jobCompleted_mutex;
273
+ pthread_cond_t jobCompleted_cond;
274
+ size_t targetSectionSize;
275
+ size_t marginSize;
276
+ size_t inBuffSize;
277
+ size_t dictSize;
278
+ size_t targetDictSize;
279
+ inBuff_t inBuff;
280
+ ZSTD_parameters params;
281
+ XXH64_state_t xxhState;
282
+ unsigned nbThreads;
283
+ unsigned jobIDMask;
284
+ unsigned doneJobID;
285
+ unsigned nextJobID;
286
+ unsigned frameEnded;
287
+ unsigned allJobsCompleted;
288
+ unsigned overlapRLog;
289
+ unsigned long long frameContentSize;
290
+ size_t sectionSize;
291
+ ZSTD_CDict* cdict;
292
+ ZSTD_CStream* cstream;
293
+ ZSTDMT_jobDescription jobs[1]; /* variable size (must lies at the end) */
294
+ };
295
+
296
+ ZSTDMT_CCtx *ZSTDMT_createCCtx(unsigned nbThreads)
297
+ {
298
+ ZSTDMT_CCtx* cctx;
299
+ U32 const minNbJobs = nbThreads + 2;
300
+ U32 const nbJobsLog2 = ZSTD_highbit32(minNbJobs) + 1;
301
+ U32 const nbJobs = 1 << nbJobsLog2;
302
+ DEBUGLOG(5, "nbThreads : %u ; minNbJobs : %u ; nbJobsLog2 : %u ; nbJobs : %u \n",
303
+ nbThreads, minNbJobs, nbJobsLog2, nbJobs);
304
+ if ((nbThreads < 1) | (nbThreads > ZSTDMT_NBTHREADS_MAX)) return NULL;
305
+ cctx = (ZSTDMT_CCtx*) calloc(1, sizeof(ZSTDMT_CCtx) + nbJobs*sizeof(ZSTDMT_jobDescription));
306
+ if (!cctx) return NULL;
307
+ cctx->nbThreads = nbThreads;
308
+ cctx->jobIDMask = nbJobs - 1;
309
+ cctx->allJobsCompleted = 1;
310
+ cctx->sectionSize = 0;
311
+ cctx->overlapRLog = 3;
312
+ cctx->factory = POOL_create(nbThreads, 1);
313
+ cctx->buffPool = ZSTDMT_createBufferPool(nbThreads);
314
+ cctx->cctxPool = ZSTDMT_createCCtxPool(nbThreads);
315
+ if (!cctx->factory | !cctx->buffPool | !cctx->cctxPool) { /* one object was not created */
316
+ ZSTDMT_freeCCtx(cctx);
317
+ return NULL;
318
+ }
319
+ if (nbThreads==1) {
320
+ cctx->cstream = ZSTD_createCStream();
321
+ if (!cctx->cstream) {
322
+ ZSTDMT_freeCCtx(cctx); return NULL;
323
+ } }
324
+ pthread_mutex_init(&cctx->jobCompleted_mutex, NULL); /* Todo : check init function return */
325
+ pthread_cond_init(&cctx->jobCompleted_cond, NULL);
326
+ DEBUGLOG(4, "mt_cctx created, for %u threads \n", nbThreads);
327
+ return cctx;
328
+ }
329
+
330
+ /* ZSTDMT_releaseAllJobResources() :
331
+ * Ensure all workers are killed first. */
332
+ static void ZSTDMT_releaseAllJobResources(ZSTDMT_CCtx* mtctx)
333
+ {
334
+ unsigned jobID;
335
+ for (jobID=0; jobID <= mtctx->jobIDMask; jobID++) {
336
+ ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[jobID].dstBuff);
337
+ mtctx->jobs[jobID].dstBuff = g_nullBuffer;
338
+ ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[jobID].src);
339
+ mtctx->jobs[jobID].src = g_nullBuffer;
340
+ ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[jobID].cctx);
341
+ mtctx->jobs[jobID].cctx = NULL;
342
+ }
343
+ memset(mtctx->jobs, 0, (mtctx->jobIDMask+1)*sizeof(ZSTDMT_jobDescription));
344
+ ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->inBuff.buffer);
345
+ mtctx->inBuff.buffer = g_nullBuffer;
346
+ mtctx->allJobsCompleted = 1;
347
+ }
348
+
349
+ size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx)
350
+ {
351
+ if (mtctx==NULL) return 0; /* compatible with free on NULL */
352
+ POOL_free(mtctx->factory);
353
+ if (!mtctx->allJobsCompleted) ZSTDMT_releaseAllJobResources(mtctx); /* stop workers first */
354
+ ZSTDMT_freeBufferPool(mtctx->buffPool); /* release job resources into pools first */
355
+ ZSTDMT_freeCCtxPool(mtctx->cctxPool);
356
+ ZSTD_freeCDict(mtctx->cdict);
357
+ ZSTD_freeCStream(mtctx->cstream);
358
+ pthread_mutex_destroy(&mtctx->jobCompleted_mutex);
359
+ pthread_cond_destroy(&mtctx->jobCompleted_cond);
360
+ free(mtctx);
361
+ return 0;
362
+ }
363
+
364
+ size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSDTMT_parameter parameter, unsigned value)
365
+ {
366
+ switch(parameter)
367
+ {
368
+ case ZSTDMT_p_sectionSize :
369
+ mtctx->sectionSize = value;
370
+ return 0;
371
+ case ZSTDMT_p_overlapSectionLog :
372
+ DEBUGLOG(4, "ZSTDMT_p_overlapSectionLog : %u", value);
373
+ mtctx->overlapRLog = (value >= 9) ? 0 : 9 - value;
374
+ return 0;
375
+ default :
376
+ return ERROR(compressionParameter_unsupported);
377
+ }
378
+ }
379
+
380
+
381
+ /* ------------------------------------------ */
382
+ /* ===== Multi-threaded compression ===== */
383
+ /* ------------------------------------------ */
384
+
385
+ size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
386
+ void* dst, size_t dstCapacity,
387
+ const void* src, size_t srcSize,
388
+ int compressionLevel)
389
+ {
390
+ ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, 0);
391
+ size_t const chunkTargetSize = (size_t)1 << (params.cParams.windowLog + 2);
392
+ unsigned const nbChunksMax = (unsigned)(srcSize / chunkTargetSize) + (srcSize < chunkTargetSize) /* min 1 */;
393
+ unsigned nbChunks = MIN(nbChunksMax, mtctx->nbThreads);
394
+ size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks;
395
+ size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0xFFFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */
396
+ size_t remainingSrcSize = srcSize;
397
+ const char* const srcStart = (const char*)src;
398
+ size_t frameStartPos = 0;
399
+
400
+ DEBUGLOG(3, "windowLog : %2u => chunkTargetSize : %u bytes ", params.cParams.windowLog, (U32)chunkTargetSize);
401
+ DEBUGLOG(2, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize);
402
+ params.fParams.contentSizeFlag = 1;
403
+
404
+ if (nbChunks==1) { /* fallback to single-thread mode */
405
+ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0];
406
+ return ZSTD_compressCCtx(cctx, dst, dstCapacity, src, srcSize, compressionLevel);
407
+ }
408
+
409
+ { unsigned u;
410
+ for (u=0; u<nbChunks; u++) {
411
+ size_t const chunkSize = MIN(remainingSrcSize, avgChunkSize);
412
+ size_t const dstBufferCapacity = u ? ZSTD_compressBound(chunkSize) : dstCapacity;
413
+ buffer_t const dstAsBuffer = { dst, dstCapacity };
414
+ buffer_t const dstBuffer = u ? ZSTDMT_getBuffer(mtctx->buffPool, dstBufferCapacity) : dstAsBuffer;
415
+ ZSTD_CCtx* const cctx = ZSTDMT_getCCtx(mtctx->cctxPool);
416
+
417
+ if ((cctx==NULL) || (dstBuffer.start==NULL)) {
418
+ mtctx->jobs[u].cSize = ERROR(memory_allocation); /* job result */
419
+ mtctx->jobs[u].jobCompleted = 1;
420
+ nbChunks = u+1;
421
+ break; /* let's wait for previous jobs to complete, but don't start new ones */
422
+ }
423
+
424
+ mtctx->jobs[u].srcStart = srcStart + frameStartPos;
425
+ mtctx->jobs[u].srcSize = chunkSize;
426
+ mtctx->jobs[u].fullFrameSize = srcSize;
427
+ mtctx->jobs[u].params = params;
428
+ mtctx->jobs[u].dstBuff = dstBuffer;
429
+ mtctx->jobs[u].cctx = cctx;
430
+ mtctx->jobs[u].firstChunk = (u==0);
431
+ mtctx->jobs[u].lastChunk = (u==nbChunks-1);
432
+ mtctx->jobs[u].jobCompleted = 0;
433
+ mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex;
434
+ mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond;
435
+
436
+ DEBUGLOG(3, "posting job %u (%u bytes)", u, (U32)chunkSize);
437
+ DEBUG_PRINTHEX(3, mtctx->jobs[u].srcStart, 12);
438
+ POOL_add(mtctx->factory, ZSTDMT_compressChunk, &mtctx->jobs[u]);
439
+
440
+ frameStartPos += chunkSize;
441
+ remainingSrcSize -= chunkSize;
442
+ } }
443
+ /* note : since nbChunks <= nbThreads, all jobs should be running immediately in parallel */
444
+
445
+ { unsigned chunkID;
446
+ size_t error = 0, dstPos = 0;
447
+ for (chunkID=0; chunkID<nbChunks; chunkID++) {
448
+ DEBUGLOG(3, "waiting for chunk %u ", chunkID);
449
+ PTHREAD_MUTEX_LOCK(&mtctx->jobCompleted_mutex);
450
+ while (mtctx->jobs[chunkID].jobCompleted==0) {
451
+ DEBUGLOG(4, "waiting for jobCompleted signal from chunk %u", chunkID);
452
+ pthread_cond_wait(&mtctx->jobCompleted_cond, &mtctx->jobCompleted_mutex);
453
+ }
454
+ pthread_mutex_unlock(&mtctx->jobCompleted_mutex);
455
+ DEBUGLOG(3, "ready to write chunk %u ", chunkID);
456
+
457
+ ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[chunkID].cctx);
458
+ mtctx->jobs[chunkID].cctx = NULL;
459
+ mtctx->jobs[chunkID].srcStart = NULL;
460
+ { size_t const cSize = mtctx->jobs[chunkID].cSize;
461
+ if (ZSTD_isError(cSize)) error = cSize;
462
+ if ((!error) && (dstPos + cSize > dstCapacity)) error = ERROR(dstSize_tooSmall);
463
+ if (chunkID) { /* note : chunk 0 is already written directly into dst */
464
+ if (!error) memcpy((char*)dst + dstPos, mtctx->jobs[chunkID].dstBuff.start, cSize);
465
+ ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[chunkID].dstBuff);
466
+ mtctx->jobs[chunkID].dstBuff = g_nullBuffer;
467
+ }
468
+ dstPos += cSize ;
469
+ }
470
+ }
471
+ if (!error) DEBUGLOG(3, "compressed size : %u ", (U32)dstPos);
472
+ return error ? error : dstPos;
473
+ }
474
+
475
+ }
476
+
477
+
478
+ /* ====================================== */
479
+ /* ======= Streaming API ======= */
480
+ /* ====================================== */
481
+
482
+ static void ZSTDMT_waitForAllJobsCompleted(ZSTDMT_CCtx* zcs) {
483
+ while (zcs->doneJobID < zcs->nextJobID) {
484
+ unsigned const jobID = zcs->doneJobID & zcs->jobIDMask;
485
+ PTHREAD_MUTEX_LOCK(&zcs->jobCompleted_mutex);
486
+ while (zcs->jobs[jobID].jobCompleted==0) {
487
+ DEBUGLOG(4, "waiting for jobCompleted signal from chunk %u", zcs->doneJobID); /* we want to block when waiting for data to flush */
488
+ pthread_cond_wait(&zcs->jobCompleted_cond, &zcs->jobCompleted_mutex);
489
+ }
490
+ pthread_mutex_unlock(&zcs->jobCompleted_mutex);
491
+ zcs->doneJobID++;
492
+ }
493
+ }
494
+
495
+
496
+ static size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs,
497
+ const void* dict, size_t dictSize, unsigned updateDict,
498
+ ZSTD_parameters params, unsigned long long pledgedSrcSize)
499
+ {
500
+ ZSTD_customMem const cmem = { NULL, NULL, NULL };
501
+ DEBUGLOG(3, "Started new compression, with windowLog : %u", params.cParams.windowLog);
502
+ if (zcs->nbThreads==1) return ZSTD_initCStream_advanced(zcs->cstream, dict, dictSize, params, pledgedSrcSize);
503
+ if (zcs->allJobsCompleted == 0) { /* previous job not correctly finished */
504
+ ZSTDMT_waitForAllJobsCompleted(zcs);
505
+ ZSTDMT_releaseAllJobResources(zcs);
506
+ zcs->allJobsCompleted = 1;
507
+ }
508
+ zcs->params = params;
509
+ if (updateDict) {
510
+ ZSTD_freeCDict(zcs->cdict); zcs->cdict = NULL;
511
+ if (dict && dictSize) {
512
+ zcs->cdict = ZSTD_createCDict_advanced(dict, dictSize, 0, params, cmem);
513
+ if (zcs->cdict == NULL) return ERROR(memory_allocation);
514
+ } }
515
+ zcs->frameContentSize = pledgedSrcSize;
516
+ zcs->targetDictSize = (zcs->overlapRLog>=9) ? 0 : (size_t)1 << (zcs->params.cParams.windowLog - zcs->overlapRLog);
517
+ DEBUGLOG(4, "overlapRLog : %u ", zcs->overlapRLog);
518
+ DEBUGLOG(3, "overlap Size : %u KB", (U32)(zcs->targetDictSize>>10));
519
+ zcs->targetSectionSize = zcs->sectionSize ? zcs->sectionSize : (size_t)1 << (zcs->params.cParams.windowLog + 2);
520
+ zcs->targetSectionSize = MAX(ZSTDMT_SECTION_SIZE_MIN, zcs->targetSectionSize);
521
+ zcs->targetSectionSize = MAX(zcs->targetDictSize, zcs->targetSectionSize);
522
+ DEBUGLOG(3, "Section Size : %u KB", (U32)(zcs->targetSectionSize>>10));
523
+ zcs->marginSize = zcs->targetSectionSize >> 2;
524
+ zcs->inBuffSize = zcs->targetDictSize + zcs->targetSectionSize + zcs->marginSize;
525
+ zcs->inBuff.buffer = ZSTDMT_getBuffer(zcs->buffPool, zcs->inBuffSize);
526
+ if (zcs->inBuff.buffer.start == NULL) return ERROR(memory_allocation);
527
+ zcs->inBuff.filled = 0;
528
+ zcs->dictSize = 0;
529
+ zcs->doneJobID = 0;
530
+ zcs->nextJobID = 0;
531
+ zcs->frameEnded = 0;
532
+ zcs->allJobsCompleted = 0;
533
+ if (params.fParams.checksumFlag) XXH64_reset(&zcs->xxhState, 0);
534
+ return 0;
535
+ }
536
+
537
+ size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* zcs,
538
+ const void* dict, size_t dictSize,
539
+ ZSTD_parameters params, unsigned long long pledgedSrcSize)
540
+ {
541
+ return ZSTDMT_initCStream_internal(zcs, dict, dictSize, 1, params, pledgedSrcSize);
542
+ }
543
+
544
+ /* ZSTDMT_resetCStream() :
545
+ * pledgedSrcSize is optional and can be zero == unknown */
546
+ size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* zcs, unsigned long long pledgedSrcSize)
547
+ {
548
+ if (zcs->nbThreads==1) return ZSTD_resetCStream(zcs->cstream, pledgedSrcSize);
549
+ return ZSTDMT_initCStream_internal(zcs, NULL, 0, 0, zcs->params, pledgedSrcSize);
550
+ }
551
+
552
+ size_t ZSTDMT_initCStream(ZSTDMT_CCtx* zcs, int compressionLevel) {
553
+ ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0);
554
+ return ZSTDMT_initCStream_internal(zcs, NULL, 0, 1, params, 0);
555
+ }
556
+
557
+
558
+ static size_t ZSTDMT_createCompressionJob(ZSTDMT_CCtx* zcs, size_t srcSize, unsigned endFrame)
559
+ {
560
+ size_t const dstBufferCapacity = ZSTD_compressBound(srcSize);
561
+ buffer_t const dstBuffer = ZSTDMT_getBuffer(zcs->buffPool, dstBufferCapacity);
562
+ ZSTD_CCtx* const cctx = ZSTDMT_getCCtx(zcs->cctxPool);
563
+ unsigned const jobID = zcs->nextJobID & zcs->jobIDMask;
564
+
565
+ if ((cctx==NULL) || (dstBuffer.start==NULL)) {
566
+ zcs->jobs[jobID].jobCompleted = 1;
567
+ zcs->nextJobID++;
568
+ ZSTDMT_waitForAllJobsCompleted(zcs);
569
+ ZSTDMT_releaseAllJobResources(zcs);
570
+ return ERROR(memory_allocation);
571
+ }
572
+
573
+ DEBUGLOG(4, "preparing job %u to compress %u bytes with %u preload ", zcs->nextJobID, (U32)srcSize, (U32)zcs->dictSize);
574
+ zcs->jobs[jobID].src = zcs->inBuff.buffer;
575
+ zcs->jobs[jobID].srcStart = zcs->inBuff.buffer.start;
576
+ zcs->jobs[jobID].srcSize = srcSize;
577
+ zcs->jobs[jobID].dictSize = zcs->dictSize; /* note : zcs->inBuff.filled is presumed >= srcSize + dictSize */
578
+ zcs->jobs[jobID].params = zcs->params;
579
+ if (zcs->nextJobID) zcs->jobs[jobID].params.fParams.checksumFlag = 0; /* do not calculate checksum within sections, just keep it in header for first section */
580
+ zcs->jobs[jobID].cdict = zcs->nextJobID==0 ? zcs->cdict : NULL;
581
+ zcs->jobs[jobID].fullFrameSize = zcs->frameContentSize;
582
+ zcs->jobs[jobID].dstBuff = dstBuffer;
583
+ zcs->jobs[jobID].cctx = cctx;
584
+ zcs->jobs[jobID].firstChunk = (zcs->nextJobID==0);
585
+ zcs->jobs[jobID].lastChunk = endFrame;
586
+ zcs->jobs[jobID].jobCompleted = 0;
587
+ zcs->jobs[jobID].dstFlushed = 0;
588
+ zcs->jobs[jobID].jobCompleted_mutex = &zcs->jobCompleted_mutex;
589
+ zcs->jobs[jobID].jobCompleted_cond = &zcs->jobCompleted_cond;
590
+
591
+ /* get a new buffer for next input */
592
+ if (!endFrame) {
593
+ size_t const newDictSize = MIN(srcSize + zcs->dictSize, zcs->targetDictSize);
594
+ zcs->inBuff.buffer = ZSTDMT_getBuffer(zcs->buffPool, zcs->inBuffSize);
595
+ if (zcs->inBuff.buffer.start == NULL) { /* not enough memory to allocate next input buffer */
596
+ zcs->jobs[jobID].jobCompleted = 1;
597
+ zcs->nextJobID++;
598
+ ZSTDMT_waitForAllJobsCompleted(zcs);
599
+ ZSTDMT_releaseAllJobResources(zcs);
600
+ return ERROR(memory_allocation);
601
+ }
602
+ DEBUGLOG(5, "inBuff filled to %u", (U32)zcs->inBuff.filled);
603
+ zcs->inBuff.filled -= srcSize + zcs->dictSize - newDictSize;
604
+ DEBUGLOG(5, "new job : filled to %u, with %u dict and %u src", (U32)zcs->inBuff.filled, (U32)newDictSize, (U32)(zcs->inBuff.filled - newDictSize));
605
+ memmove(zcs->inBuff.buffer.start, (const char*)zcs->jobs[jobID].srcStart + zcs->dictSize + srcSize - newDictSize, zcs->inBuff.filled);
606
+ DEBUGLOG(5, "new inBuff pre-filled");
607
+ zcs->dictSize = newDictSize;
608
+ } else {
609
+ zcs->inBuff.buffer = g_nullBuffer;
610
+ zcs->inBuff.filled = 0;
611
+ zcs->dictSize = 0;
612
+ zcs->frameEnded = 1;
613
+ if (zcs->nextJobID == 0)
614
+ zcs->params.fParams.checksumFlag = 0; /* single chunk : checksum is calculated directly within worker thread */
615
+ }
616
+
617
+ DEBUGLOG(3, "posting job %u : %u bytes (end:%u) (note : doneJob = %u=>%u)", zcs->nextJobID, (U32)zcs->jobs[jobID].srcSize, zcs->jobs[jobID].lastChunk, zcs->doneJobID, zcs->doneJobID & zcs->jobIDMask);
618
+ POOL_add(zcs->factory, ZSTDMT_compressChunk, &zcs->jobs[jobID]); /* this call is blocking when thread worker pool is exhausted */
619
+ zcs->nextJobID++;
620
+ return 0;
621
+ }
622
+
623
+
624
+ /* ZSTDMT_flushNextJob() :
625
+ * output : will be updated with amount of data flushed .
626
+ * blockToFlush : if >0, the function will block and wait if there is no data available to flush .
627
+ * @return : amount of data remaining within internal buffer, 1 if unknown but > 0, 0 if no more, or an error code */
628
+ static size_t ZSTDMT_flushNextJob(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, unsigned blockToFlush)
629
+ {
630
+ unsigned const wJobID = zcs->doneJobID & zcs->jobIDMask;
631
+ if (zcs->doneJobID == zcs->nextJobID) return 0; /* all flushed ! */
632
+ PTHREAD_MUTEX_LOCK(&zcs->jobCompleted_mutex);
633
+ while (zcs->jobs[wJobID].jobCompleted==0) {
634
+ DEBUGLOG(5, "waiting for jobCompleted signal from job %u", zcs->doneJobID);
635
+ if (!blockToFlush) { pthread_mutex_unlock(&zcs->jobCompleted_mutex); return 0; } /* nothing ready to be flushed => skip */
636
+ pthread_cond_wait(&zcs->jobCompleted_cond, &zcs->jobCompleted_mutex); /* block when nothing available to flush */
637
+ }
638
+ pthread_mutex_unlock(&zcs->jobCompleted_mutex);
639
+ /* compression job completed : output can be flushed */
640
+ { ZSTDMT_jobDescription job = zcs->jobs[wJobID];
641
+ if (!job.jobScanned) {
642
+ if (ZSTD_isError(job.cSize)) {
643
+ DEBUGLOG(5, "compression error detected ");
644
+ ZSTDMT_waitForAllJobsCompleted(zcs);
645
+ ZSTDMT_releaseAllJobResources(zcs);
646
+ return job.cSize;
647
+ }
648
+ ZSTDMT_releaseCCtx(zcs->cctxPool, job.cctx);
649
+ zcs->jobs[wJobID].cctx = NULL;
650
+ DEBUGLOG(5, "zcs->params.fParams.checksumFlag : %u ", zcs->params.fParams.checksumFlag);
651
+ if (zcs->params.fParams.checksumFlag) {
652
+ XXH64_update(&zcs->xxhState, (const char*)job.srcStart + job.dictSize, job.srcSize);
653
+ if (zcs->frameEnded && (zcs->doneJobID+1 == zcs->nextJobID)) { /* write checksum at end of last section */
654
+ U32 const checksum = (U32)XXH64_digest(&zcs->xxhState);
655
+ DEBUGLOG(4, "writing checksum : %08X \n", checksum);
656
+ MEM_writeLE32((char*)job.dstBuff.start + job.cSize, checksum);
657
+ job.cSize += 4;
658
+ zcs->jobs[wJobID].cSize += 4;
659
+ } }
660
+ ZSTDMT_releaseBuffer(zcs->buffPool, job.src);
661
+ zcs->jobs[wJobID].srcStart = NULL;
662
+ zcs->jobs[wJobID].src = g_nullBuffer;
663
+ zcs->jobs[wJobID].jobScanned = 1;
664
+ }
665
+ { size_t const toWrite = MIN(job.cSize - job.dstFlushed, output->size - output->pos);
666
+ DEBUGLOG(4, "Flushing %u bytes from job %u ", (U32)toWrite, zcs->doneJobID);
667
+ memcpy((char*)output->dst + output->pos, (const char*)job.dstBuff.start + job.dstFlushed, toWrite);
668
+ output->pos += toWrite;
669
+ job.dstFlushed += toWrite;
670
+ }
671
+ if (job.dstFlushed == job.cSize) { /* output buffer fully flushed => move to next one */
672
+ ZSTDMT_releaseBuffer(zcs->buffPool, job.dstBuff);
673
+ zcs->jobs[wJobID].dstBuff = g_nullBuffer;
674
+ zcs->jobs[wJobID].jobCompleted = 0;
675
+ zcs->doneJobID++;
676
+ } else {
677
+ zcs->jobs[wJobID].dstFlushed = job.dstFlushed;
678
+ }
679
+ /* return value : how many bytes left in buffer ; fake it to 1 if unknown but >0 */
680
+ if (job.cSize > job.dstFlushed) return (job.cSize - job.dstFlushed);
681
+ if (zcs->doneJobID < zcs->nextJobID) return 1; /* still some buffer to flush */
682
+ zcs->allJobsCompleted = zcs->frameEnded; /* frame completed and entirely flushed */
683
+ return 0; /* everything flushed */
684
+ } }
685
+
686
+
687
+ size_t ZSTDMT_compressStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
688
+ {
689
+ size_t const newJobThreshold = zcs->dictSize + zcs->targetSectionSize + zcs->marginSize;
690
+ if (zcs->frameEnded) return ERROR(stage_wrong); /* current frame being ended. Only flush is allowed. Restart with init */
691
+ if (zcs->nbThreads==1) return ZSTD_compressStream(zcs->cstream, output, input);
692
+
693
+ /* fill input buffer */
694
+ { size_t const toLoad = MIN(input->size - input->pos, zcs->inBuffSize - zcs->inBuff.filled);
695
+ memcpy((char*)zcs->inBuff.buffer.start + zcs->inBuff.filled, input->src, toLoad);
696
+ input->pos += toLoad;
697
+ zcs->inBuff.filled += toLoad;
698
+ }
699
+
700
+ if ( (zcs->inBuff.filled >= newJobThreshold) /* filled enough : let's compress */
701
+ && (zcs->nextJobID <= zcs->doneJobID + zcs->jobIDMask) ) { /* avoid overwriting job round buffer */
702
+ CHECK_F( ZSTDMT_createCompressionJob(zcs, zcs->targetSectionSize, 0) );
703
+ }
704
+
705
+ /* check for data to flush */
706
+ CHECK_F( ZSTDMT_flushNextJob(zcs, output, (zcs->inBuff.filled == zcs->inBuffSize)) ); /* block if it wasn't possible to create new job due to saturation */
707
+
708
+ /* recommended next input size : fill current input buffer */
709
+ return zcs->inBuffSize - zcs->inBuff.filled; /* note : could be zero when input buffer is fully filled and no more availability to create new job */
710
+ }
711
+
712
+
713
+ static size_t ZSTDMT_flushStream_internal(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output, unsigned endFrame)
714
+ {
715
+ size_t const srcSize = zcs->inBuff.filled - zcs->dictSize;
716
+
717
+ if (srcSize) DEBUGLOG(4, "flushing : %u bytes left to compress", (U32)srcSize);
718
+ if ( ((srcSize > 0) || (endFrame && !zcs->frameEnded))
719
+ && (zcs->nextJobID <= zcs->doneJobID + zcs->jobIDMask) ) {
720
+ CHECK_F( ZSTDMT_createCompressionJob(zcs, srcSize, endFrame) );
721
+ }
722
+
723
+ /* check if there is any data available to flush */
724
+ DEBUGLOG(5, "zcs->doneJobID : %u ; zcs->nextJobID : %u ", zcs->doneJobID, zcs->nextJobID);
725
+ return ZSTDMT_flushNextJob(zcs, output, 1);
726
+ }
727
+
728
+
729
+ size_t ZSTDMT_flushStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output)
730
+ {
731
+ if (zcs->nbThreads==1) return ZSTD_flushStream(zcs->cstream, output);
732
+ return ZSTDMT_flushStream_internal(zcs, output, 0);
733
+ }
734
+
735
+ size_t ZSTDMT_endStream(ZSTDMT_CCtx* zcs, ZSTD_outBuffer* output)
736
+ {
737
+ if (zcs->nbThreads==1) return ZSTD_endStream(zcs->cstream, output);
738
+ return ZSTDMT_flushStream_internal(zcs, output, 1);
739
+ }