zstd-ruby 1.3.3.0 → 1.3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +1 -1
- data/ext/zstdruby/libzstd/BUCK +13 -0
- data/ext/zstdruby/libzstd/README.md +32 -25
- data/ext/zstdruby/libzstd/common/bitstream.h +1 -1
- data/ext/zstdruby/libzstd/common/compiler.h +25 -0
- data/ext/zstdruby/libzstd/common/cpu.h +216 -0
- data/ext/zstdruby/libzstd/common/error_private.c +1 -0
- data/ext/zstdruby/libzstd/common/fse.h +1 -1
- data/ext/zstdruby/libzstd/common/fse_decompress.c +2 -2
- data/ext/zstdruby/libzstd/common/huf.h +114 -89
- data/ext/zstdruby/libzstd/common/pool.c +46 -17
- data/ext/zstdruby/libzstd/common/pool.h +18 -9
- data/ext/zstdruby/libzstd/common/threading.h +12 -12
- data/ext/zstdruby/libzstd/common/zstd_errors.h +16 -7
- data/ext/zstdruby/libzstd/common/zstd_internal.h +4 -5
- data/ext/zstdruby/libzstd/compress/fse_compress.c +19 -11
- data/ext/zstdruby/libzstd/compress/huf_compress.c +160 -62
- data/ext/zstdruby/libzstd/compress/zstd_compress.c +973 -644
- data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +281 -34
- data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +80 -62
- data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +11 -4
- data/ext/zstdruby/libzstd/compress/zstd_fast.c +87 -71
- data/ext/zstdruby/libzstd/compress/zstd_fast.h +10 -6
- data/ext/zstdruby/libzstd/compress/zstd_lazy.c +333 -274
- data/ext/zstdruby/libzstd/compress/zstd_lazy.h +33 -16
- data/ext/zstdruby/libzstd/compress/zstd_ldm.c +305 -359
- data/ext/zstdruby/libzstd/compress/zstd_ldm.h +64 -21
- data/ext/zstdruby/libzstd/compress/zstd_opt.c +194 -56
- data/ext/zstdruby/libzstd/compress/zstd_opt.h +17 -5
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +1131 -449
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +32 -16
- data/ext/zstdruby/libzstd/decompress/huf_decompress.c +390 -290
- data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +777 -439
- data/ext/zstdruby/libzstd/dictBuilder/cover.c +11 -8
- data/ext/zstdruby/libzstd/dictBuilder/zdict.c +83 -50
- data/ext/zstdruby/libzstd/dictBuilder/zdict.h +44 -43
- data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +2 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v04.c +42 -118
- data/ext/zstdruby/libzstd/legacy/zstd_v06.c +2 -2
- data/ext/zstdruby/libzstd/legacy/zstd_v07.c +2 -2
- data/ext/zstdruby/libzstd/zstd.h +254 -254
- data/lib/zstd-ruby/version.rb +1 -1
- metadata +4 -3
@@ -58,32 +58,32 @@ extern "C" {
|
|
58
58
|
#endif
|
59
59
|
|
60
60
|
|
61
|
-
/*
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
61
|
+
/* ========================== */
|
62
|
+
/* *** simple functions *** */
|
63
|
+
/* ========================== */
|
64
|
+
|
65
|
+
/** HUF_compress() :
|
66
|
+
* Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
|
67
|
+
* 'dst' buffer must be already allocated.
|
68
|
+
* Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
|
69
|
+
* `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
|
70
|
+
* @return : size of compressed data (<= `dstCapacity`).
|
71
|
+
* Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
|
72
|
+
* if HUF_isError(return), compression failed (more details using HUF_getErrorName())
|
73
|
+
*/
|
73
74
|
HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
|
74
75
|
const void* src, size_t srcSize);
|
75
76
|
|
76
|
-
/**
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
*/
|
77
|
+
/** HUF_decompress() :
|
78
|
+
* Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
|
79
|
+
* into already allocated buffer 'dst', of minimum size 'dstSize'.
|
80
|
+
* `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
|
81
|
+
* Note : in contrast with FSE, HUF_decompress can regenerate
|
82
|
+
* RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
|
83
|
+
* because it knows size to regenerate (originalSize).
|
84
|
+
* @return : size of regenerated data (== originalSize),
|
85
|
+
* or an error code, which can be tested using HUF_isError()
|
86
|
+
*/
|
87
87
|
HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,
|
88
88
|
const void* cSrc, size_t cSrcSize);
|
89
89
|
|
@@ -100,39 +100,32 @@ HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error c
|
|
100
100
|
/* *** Advanced function *** */
|
101
101
|
|
102
102
|
/** HUF_compress2() :
|
103
|
-
* Same as HUF_compress(), but offers
|
104
|
-
*
|
105
|
-
|
103
|
+
* Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
|
104
|
+
* `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
|
105
|
+
* `tableLog` must be `<= HUF_TABLELOG_MAX` . */
|
106
|
+
HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
|
107
|
+
const void* src, size_t srcSize,
|
108
|
+
unsigned maxSymbolValue, unsigned tableLog);
|
106
109
|
|
107
110
|
/** HUF_compress4X_wksp() :
|
108
111
|
* Same as HUF_compress2(), but uses externally allocated `workSpace`.
|
109
|
-
*
|
112
|
+
* `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
|
110
113
|
#define HUF_WORKSPACE_SIZE (6 << 10)
|
111
114
|
#define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
|
112
|
-
HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
* HUF_readDTableX2_wksp() and HUF_readDTableX4_wksp().
|
117
|
-
*
|
118
|
-
* The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
|
119
|
-
* HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
|
120
|
-
* Buffer overflow errors may potentially occur if code modifications result in
|
121
|
-
* a required workspace size greater than that specified in the following
|
122
|
-
* macro.
|
123
|
-
*/
|
124
|
-
#define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
|
125
|
-
#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
|
115
|
+
HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
|
116
|
+
const void* src, size_t srcSize,
|
117
|
+
unsigned maxSymbolValue, unsigned tableLog,
|
118
|
+
void* workSpace, size_t wkspSize);
|
126
119
|
|
127
120
|
#endif /* HUF_H_298734234 */
|
128
121
|
|
129
122
|
/* ******************************************************************
|
130
123
|
* WARNING !!
|
131
124
|
* The following section contains advanced and experimental definitions
|
132
|
-
* which shall never be used in the context of
|
125
|
+
* which shall never be used in the context of a dynamic library,
|
133
126
|
* because they are not guaranteed to remain stable in the future.
|
134
127
|
* Only consider them in association with static linking.
|
135
|
-
|
128
|
+
* *****************************************************************/
|
136
129
|
#if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
|
137
130
|
#define HUF_H_HUF_STATIC_LINKING_ONLY
|
138
131
|
|
@@ -141,11 +134,11 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity, const
|
|
141
134
|
|
142
135
|
|
143
136
|
/* *** Constants *** */
|
144
|
-
#define HUF_TABLELOG_MAX 12
|
145
|
-
#define HUF_TABLELOG_DEFAULT 11
|
137
|
+
#define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
|
138
|
+
#define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
|
146
139
|
#define HUF_SYMBOLVALUE_MAX 255
|
147
140
|
|
148
|
-
#define HUF_TABLELOG_ABSOLUTEMAX 15
|
141
|
+
#define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
|
149
142
|
#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
|
150
143
|
# error "HUF_TABLELOG_MAX is too large !"
|
151
144
|
#endif
|
@@ -192,24 +185,23 @@ size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
|
|
192
185
|
|
193
186
|
|
194
187
|
/* ****************************************
|
195
|
-
* HUF detailed API
|
196
|
-
|
197
|
-
|
198
|
-
HUF_compress() does the following:
|
199
|
-
1. count symbol occurrence from source[] into table count[] using FSE_count()
|
200
|
-
2. (optional) refine tableLog using HUF_optimalTableLog()
|
201
|
-
3. build Huffman table from count using HUF_buildCTable()
|
202
|
-
4. save Huffman table to memory buffer using HUF_writeCTable()
|
203
|
-
5. encode the data stream using HUF_compress4X_usingCTable()
|
204
|
-
|
205
|
-
The following API allows targeting specific sub-functions for advanced tasks.
|
206
|
-
For example, it's possible to compress several blocks using the same 'CTable',
|
207
|
-
or to save and regenerate 'CTable' using external methods.
|
208
|
-
*/
|
209
|
-
/* FSE_count() : find it within "fse.h" */
|
188
|
+
* HUF detailed API
|
189
|
+
* ****************************************/
|
190
|
+
|
191
|
+
/*! HUF_compress() does the following:
|
192
|
+
* 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
|
193
|
+
* 2. (optional) refine tableLog using HUF_optimalTableLog()
|
194
|
+
* 3. build Huffman table from count using HUF_buildCTable()
|
195
|
+
* 4. save Huffman table to memory buffer using HUF_writeCTable()
|
196
|
+
* 5. encode the data stream using HUF_compress4X_usingCTable()
|
197
|
+
*
|
198
|
+
* The following API allows targeting specific sub-functions for advanced tasks.
|
199
|
+
* For example, it's possible to compress several blocks using the same 'CTable',
|
200
|
+
* or to save and regenerate 'CTable' using external methods.
|
201
|
+
*/
|
210
202
|
unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
|
211
203
|
typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
|
212
|
-
size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);
|
204
|
+
size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
|
213
205
|
size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
|
214
206
|
size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
|
215
207
|
|
@@ -219,46 +211,65 @@ typedef enum {
|
|
219
211
|
HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */
|
220
212
|
} HUF_repeat;
|
221
213
|
/** HUF_compress4X_repeat() :
|
222
|
-
*
|
223
|
-
*
|
224
|
-
*
|
225
|
-
*
|
226
|
-
size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
|
214
|
+
* Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
|
215
|
+
* If it uses hufTable it does not modify hufTable or repeat.
|
216
|
+
* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
|
217
|
+
* If preferRepeat then the old table will always be used if valid. */
|
218
|
+
size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
|
219
|
+
const void* src, size_t srcSize,
|
220
|
+
unsigned maxSymbolValue, unsigned tableLog,
|
221
|
+
void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
|
222
|
+
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
|
227
223
|
|
228
224
|
/** HUF_buildCTable_wksp() :
|
229
225
|
* Same as HUF_buildCTable(), but using externally allocated scratch buffer.
|
230
|
-
*
|
226
|
+
* `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
|
231
227
|
*/
|
228
|
+
#define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
|
229
|
+
#define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
|
232
230
|
size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize);
|
233
231
|
|
234
232
|
/*! HUF_readStats() :
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
|
240
|
-
U32* nbSymbolsPtr, U32* tableLogPtr,
|
233
|
+
* Read compact Huffman tree, saved by HUF_writeCTable().
|
234
|
+
* `huffWeight` is destination buffer.
|
235
|
+
* @return : size read from `src` , or an error Code .
|
236
|
+
* Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
|
237
|
+
size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
|
238
|
+
U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
|
241
239
|
const void* src, size_t srcSize);
|
242
240
|
|
243
241
|
/** HUF_readCTable() :
|
244
|
-
*
|
242
|
+
* Loading a CTable saved with HUF_writeCTable() */
|
245
243
|
size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
|
246
244
|
|
247
245
|
|
248
246
|
/*
|
249
|
-
HUF_decompress() does the following:
|
250
|
-
1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
|
251
|
-
2. build Huffman table from save, using
|
252
|
-
3. decode 1 or 4 segments in parallel using
|
253
|
-
*/
|
247
|
+
* HUF_decompress() does the following:
|
248
|
+
* 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
|
249
|
+
* 2. build Huffman table from save, using HUF_readDTableX?()
|
250
|
+
* 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
|
251
|
+
*/
|
254
252
|
|
255
253
|
/** HUF_selectDecoder() :
|
256
|
-
*
|
257
|
-
*
|
258
|
-
*
|
259
|
-
*
|
254
|
+
* Tells which decoder is likely to decode faster,
|
255
|
+
* based on a set of pre-computed metrics.
|
256
|
+
* @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
|
257
|
+
* Assumption : 0 < dstSize <= 128 KB */
|
260
258
|
U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
|
261
259
|
|
260
|
+
/**
|
261
|
+
* The minimum workspace size for the `workSpace` used in
|
262
|
+
* HUF_readDTableX2_wksp() and HUF_readDTableX4_wksp().
|
263
|
+
*
|
264
|
+
* The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
|
265
|
+
* HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
|
266
|
+
* Buffer overflow errors may potentially occur if code modifications result in
|
267
|
+
* a required workspace size greater than that specified in the following
|
268
|
+
* macro.
|
269
|
+
*/
|
270
|
+
#define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
|
271
|
+
#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
|
272
|
+
|
262
273
|
size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
|
263
274
|
size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
|
264
275
|
size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize);
|
@@ -269,17 +280,23 @@ size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* c
|
|
269
280
|
size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
|
270
281
|
|
271
282
|
|
283
|
+
/* ====================== */
|
272
284
|
/* single stream variants */
|
285
|
+
/* ====================== */
|
273
286
|
|
274
287
|
size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
|
275
288
|
size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
|
276
289
|
size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
|
277
290
|
/** HUF_compress1X_repeat() :
|
278
|
-
*
|
279
|
-
*
|
280
|
-
*
|
281
|
-
*
|
282
|
-
size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
|
291
|
+
* Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
|
292
|
+
* If it uses hufTable it does not modify hufTable or repeat.
|
293
|
+
* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
|
294
|
+
* If preferRepeat then the old table will always be used if valid. */
|
295
|
+
size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
|
296
|
+
const void* src, size_t srcSize,
|
297
|
+
unsigned maxSymbolValue, unsigned tableLog,
|
298
|
+
void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
|
299
|
+
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
|
283
300
|
|
284
301
|
size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
|
285
302
|
size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
|
@@ -295,6 +312,14 @@ size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cS
|
|
295
312
|
size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
|
296
313
|
size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
|
297
314
|
|
315
|
+
/* BMI2 variants.
|
316
|
+
* If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
|
317
|
+
*/
|
318
|
+
size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
|
319
|
+
size_t HUF_decompress1X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
|
320
|
+
size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
|
321
|
+
size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
|
322
|
+
|
298
323
|
#endif /* HUF_STATIC_LINKING_ONLY */
|
299
324
|
|
300
325
|
#if defined (__cplusplus)
|
@@ -12,6 +12,7 @@
|
|
12
12
|
/* ====== Dependencies ======= */
|
13
13
|
#include <stddef.h> /* size_t */
|
14
14
|
#include "pool.h"
|
15
|
+
#include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
|
15
16
|
|
16
17
|
/* ====== Compiler specifics ====== */
|
17
18
|
#if defined(_MSC_VER)
|
@@ -193,32 +194,54 @@ static int isQueueFull(POOL_ctx const* ctx) {
|
|
193
194
|
}
|
194
195
|
}
|
195
196
|
|
196
|
-
void POOL_add(void* ctxVoid, POOL_function function, void *opaque) {
|
197
|
-
POOL_ctx* const ctx = (POOL_ctx*)ctxVoid;
|
198
|
-
if (!ctx) { return; }
|
199
197
|
|
198
|
+
static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque)
|
199
|
+
{
|
200
|
+
POOL_job const job = {function, opaque};
|
201
|
+
assert(ctx != NULL);
|
202
|
+
if (ctx->shutdown) return;
|
203
|
+
|
204
|
+
ctx->queueEmpty = 0;
|
205
|
+
ctx->queue[ctx->queueTail] = job;
|
206
|
+
ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
|
207
|
+
ZSTD_pthread_cond_signal(&ctx->queuePopCond);
|
208
|
+
}
|
209
|
+
|
210
|
+
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque)
|
211
|
+
{
|
212
|
+
assert(ctx != NULL);
|
200
213
|
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
201
|
-
|
214
|
+
/* Wait until there is space in the queue for the new job */
|
215
|
+
while (isQueueFull(ctx) && (!ctx->shutdown)) {
|
216
|
+
ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
|
217
|
+
}
|
218
|
+
POOL_add_internal(ctx, function, opaque);
|
219
|
+
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
220
|
+
}
|
202
221
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
|
212
|
-
}
|
222
|
+
|
223
|
+
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
|
224
|
+
{
|
225
|
+
assert(ctx != NULL);
|
226
|
+
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
227
|
+
if (isQueueFull(ctx)) {
|
228
|
+
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
229
|
+
return 0;
|
213
230
|
}
|
231
|
+
POOL_add_internal(ctx, function, opaque);
|
214
232
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
215
|
-
|
233
|
+
return 1;
|
216
234
|
}
|
217
235
|
|
236
|
+
|
218
237
|
#else /* ZSTD_MULTITHREAD not defined */
|
238
|
+
|
239
|
+
/* ========================== */
|
219
240
|
/* No multi-threading support */
|
241
|
+
/* ========================== */
|
220
242
|
|
221
|
-
|
243
|
+
|
244
|
+
/* We don't need any data, but if it is empty, malloc() might return NULL. */
|
222
245
|
struct POOL_ctx_s {
|
223
246
|
int dummy;
|
224
247
|
};
|
@@ -240,9 +263,15 @@ void POOL_free(POOL_ctx* ctx) {
|
|
240
263
|
(void)ctx;
|
241
264
|
}
|
242
265
|
|
243
|
-
void POOL_add(
|
266
|
+
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) {
|
267
|
+
(void)ctx;
|
268
|
+
function(opaque);
|
269
|
+
}
|
270
|
+
|
271
|
+
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
|
244
272
|
(void)ctx;
|
245
273
|
function(opaque);
|
274
|
+
return 1;
|
246
275
|
}
|
247
276
|
|
248
277
|
size_t POOL_sizeof(POOL_ctx* ctx) {
|
@@ -17,7 +17,8 @@ extern "C" {
|
|
17
17
|
|
18
18
|
|
19
19
|
#include <stddef.h> /* size_t */
|
20
|
-
#
|
20
|
+
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
|
21
|
+
#include "zstd.h"
|
21
22
|
|
22
23
|
typedef struct POOL_ctx_s POOL_ctx;
|
23
24
|
|
@@ -27,35 +28,43 @@ typedef struct POOL_ctx_s POOL_ctx;
|
|
27
28
|
* The maximum number of queued jobs before blocking is `queueSize`.
|
28
29
|
* @return : POOL_ctx pointer on success, else NULL.
|
29
30
|
*/
|
30
|
-
POOL_ctx
|
31
|
+
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
|
31
32
|
|
32
|
-
POOL_ctx
|
33
|
+
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem);
|
33
34
|
|
34
35
|
/*! POOL_free() :
|
35
36
|
Free a thread pool returned by POOL_create().
|
36
37
|
*/
|
37
|
-
void POOL_free(POOL_ctx
|
38
|
+
void POOL_free(POOL_ctx* ctx);
|
38
39
|
|
39
40
|
/*! POOL_sizeof() :
|
40
41
|
return memory usage of pool returned by POOL_create().
|
41
42
|
*/
|
42
|
-
size_t POOL_sizeof(POOL_ctx
|
43
|
+
size_t POOL_sizeof(POOL_ctx* ctx);
|
43
44
|
|
44
45
|
/*! POOL_function :
|
45
46
|
The function type that can be added to a thread pool.
|
46
47
|
*/
|
47
|
-
typedef void (*POOL_function)(void
|
48
|
+
typedef void (*POOL_function)(void*);
|
48
49
|
/*! POOL_add_function :
|
49
50
|
The function type for a generic thread pool add function.
|
50
51
|
*/
|
51
|
-
typedef void (*POOL_add_function)(void
|
52
|
+
typedef void (*POOL_add_function)(void*, POOL_function, void*);
|
52
53
|
|
53
54
|
/*! POOL_add() :
|
54
|
-
Add the job `function(opaque)` to the thread pool.
|
55
|
+
Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
|
55
56
|
Possibly blocks until there is room in the queue.
|
56
57
|
Note : The function may be executed asynchronously, so `opaque` must live until the function has been completed.
|
57
58
|
*/
|
58
|
-
void POOL_add(
|
59
|
+
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
|
60
|
+
|
61
|
+
|
62
|
+
/*! POOL_tryAdd() :
|
63
|
+
Add the job `function(opaque)` to the thread pool if a worker is available.
|
64
|
+
return immediately otherwise.
|
65
|
+
@return : 1 if successful, 0 if not.
|
66
|
+
*/
|
67
|
+
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
|
59
68
|
|
60
69
|
|
61
70
|
#if defined (__cplusplus)
|
@@ -45,15 +45,15 @@ extern "C" {
|
|
45
45
|
|
46
46
|
/* mutex */
|
47
47
|
#define ZSTD_pthread_mutex_t CRITICAL_SECTION
|
48
|
-
#define ZSTD_pthread_mutex_init(a, b) (InitializeCriticalSection((a)), 0)
|
48
|
+
#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)
|
49
49
|
#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))
|
50
50
|
#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))
|
51
51
|
#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))
|
52
52
|
|
53
53
|
/* condition variable */
|
54
54
|
#define ZSTD_pthread_cond_t CONDITION_VARIABLE
|
55
|
-
#define ZSTD_pthread_cond_init(a, b) (InitializeConditionVariable((a)), 0)
|
56
|
-
#define ZSTD_pthread_cond_destroy(a)
|
55
|
+
#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)
|
56
|
+
#define ZSTD_pthread_cond_destroy(a) ((void)(a))
|
57
57
|
#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
|
58
58
|
#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))
|
59
59
|
#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))
|
@@ -100,17 +100,17 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
|
|
100
100
|
/* No multithreading support */
|
101
101
|
|
102
102
|
typedef int ZSTD_pthread_mutex_t;
|
103
|
-
#define ZSTD_pthread_mutex_init(a, b) ((void)a, 0)
|
104
|
-
#define ZSTD_pthread_mutex_destroy(a)
|
105
|
-
#define ZSTD_pthread_mutex_lock(a)
|
106
|
-
#define ZSTD_pthread_mutex_unlock(a)
|
103
|
+
#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)
|
104
|
+
#define ZSTD_pthread_mutex_destroy(a) ((void)(a))
|
105
|
+
#define ZSTD_pthread_mutex_lock(a) ((void)(a))
|
106
|
+
#define ZSTD_pthread_mutex_unlock(a) ((void)(a))
|
107
107
|
|
108
108
|
typedef int ZSTD_pthread_cond_t;
|
109
|
-
#define ZSTD_pthread_cond_init(a, b) ((void)a, 0)
|
110
|
-
#define ZSTD_pthread_cond_destroy(a)
|
111
|
-
#define ZSTD_pthread_cond_wait(a, b)
|
112
|
-
#define ZSTD_pthread_cond_signal(a)
|
113
|
-
#define ZSTD_pthread_cond_broadcast(a)
|
109
|
+
#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)
|
110
|
+
#define ZSTD_pthread_cond_destroy(a) ((void)(a))
|
111
|
+
#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))
|
112
|
+
#define ZSTD_pthread_cond_signal(a) ((void)(a))
|
113
|
+
#define ZSTD_pthread_cond_broadcast(a) ((void)(a))
|
114
114
|
|
115
115
|
/* do not use ZSTD_pthread_t */
|
116
116
|
|