zstd-ruby 1.4.5.0 → 1.5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +8 -0
  3. data/.github/workflows/ruby.yml +35 -0
  4. data/README.md +2 -2
  5. data/ext/zstdruby/extconf.rb +2 -1
  6. data/ext/zstdruby/libzstd/BUCK +5 -7
  7. data/ext/zstdruby/libzstd/Makefile +225 -222
  8. data/ext/zstdruby/libzstd/README.md +43 -5
  9. data/ext/zstdruby/libzstd/common/bitstream.h +46 -22
  10. data/ext/zstdruby/libzstd/common/compiler.h +182 -22
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +1 -1
  13. data/ext/zstdruby/libzstd/common/debug.h +12 -19
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +196 -44
  15. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  16. data/ext/zstdruby/libzstd/common/error_private.h +82 -3
  17. data/ext/zstdruby/libzstd/common/fse.h +41 -12
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +139 -22
  19. data/ext/zstdruby/libzstd/common/huf.h +47 -23
  20. data/ext/zstdruby/libzstd/common/mem.h +87 -98
  21. data/ext/zstdruby/libzstd/common/pool.c +23 -17
  22. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +131 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +6 -5
  25. data/ext/zstdruby/libzstd/common/xxhash.c +6 -846
  26. data/ext/zstdruby/libzstd/common/xxhash.h +5568 -167
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  28. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +189 -142
  30. data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
  31. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  32. data/ext/zstdruby/libzstd/compress/fse_compress.c +89 -46
  33. data/ext/zstdruby/libzstd/compress/hist.c +27 -29
  34. data/ext/zstdruby/libzstd/compress/hist.h +2 -2
  35. data/ext/zstdruby/libzstd/compress/huf_compress.c +770 -198
  36. data/ext/zstdruby/libzstd/compress/zstd_compress.c +2894 -863
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +390 -90
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +12 -11
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +4 -2
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +31 -8
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +1 -1
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +25 -297
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +1 -1
  44. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +206 -69
  45. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +307 -132
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +1 -1
  47. data/ext/zstdruby/libzstd/compress/zstd_fast.c +322 -143
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.h +1 -1
  49. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1136 -174
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +59 -1
  51. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +316 -213
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +9 -2
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  54. data/ext/zstdruby/libzstd/compress/zstd_opt.c +373 -150
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  56. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +152 -444
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +31 -113
  58. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1044 -403
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +571 -0
  60. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +9 -9
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  62. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +450 -105
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +913 -273
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +14 -5
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +59 -12
  66. data/ext/zstdruby/libzstd/deprecated/zbuff.h +1 -1
  67. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +1 -1
  68. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +24 -4
  69. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/cover.c +55 -38
  71. data/ext/zstdruby/libzstd/dictBuilder/cover.h +7 -6
  72. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  73. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +43 -34
  74. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +128 -58
  75. data/ext/zstdruby/libzstd/dll/example/Makefile +1 -1
  76. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  77. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +1 -1
  78. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +8 -8
  79. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  80. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +9 -9
  81. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  82. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +9 -9
  83. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  84. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +10 -10
  85. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  86. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +13 -13
  87. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +1 -1
  88. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +13 -13
  89. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  90. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +13 -13
  91. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  92. data/ext/zstdruby/libzstd/libzstd.mk +185 -0
  93. data/ext/zstdruby/libzstd/libzstd.pc.in +4 -3
  94. data/ext/zstdruby/libzstd/modulemap/module.modulemap +4 -0
  95. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +154 -7
  96. data/ext/zstdruby/libzstd/zstd.h +699 -214
  97. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +2 -1
  98. data/ext/zstdruby/zstdruby.c +2 -2
  99. data/lib/zstd-ruby/version.rb +1 -1
  100. metadata +15 -6
  101. data/.travis.yml +0 -14
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -18,8 +18,10 @@ extern "C" {
18
18
  /*-****************************************
19
19
  * Dependencies
20
20
  ******************************************/
21
- #include <stddef.h> /* size_t, ptrdiff_t */
22
- #include <string.h> /* memcpy */
21
+ #include <stddef.h> /* size_t, ptrdiff_t */
22
+ #include "compiler.h" /* __has_builtin */
23
+ #include "debug.h" /* DEBUG_STATIC_ASSERT */
24
+ #include "zstd_deps.h" /* ZSTD_memcpy */
23
25
 
24
26
 
25
27
  /*-****************************************
@@ -39,94 +41,18 @@ extern "C" {
39
41
  # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
40
42
  #endif
41
43
 
42
- #ifndef __has_builtin
43
- # define __has_builtin(x) 0 /* compat. with non-clang compilers */
44
- #endif
45
-
46
- /* code only tested on 32 and 64 bits systems */
47
- #define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; }
48
- MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
49
-
50
- /* detects whether we are being compiled under msan */
51
- #if defined (__has_feature)
52
- # if __has_feature(memory_sanitizer)
53
- # define MEMORY_SANITIZER 1
54
- # endif
55
- #endif
56
-
57
- #if defined (MEMORY_SANITIZER)
58
- /* Not all platforms that support msan provide sanitizers/msan_interface.h.
59
- * We therefore declare the functions we need ourselves, rather than trying to
60
- * include the header file... */
61
-
62
- #include <stdint.h> /* intptr_t */
63
-
64
- /* Make memory region fully initialized (without changing its contents). */
65
- void __msan_unpoison(const volatile void *a, size_t size);
66
-
67
- /* Make memory region fully uninitialized (without changing its contents).
68
- This is a legacy interface that does not update origin information. Use
69
- __msan_allocated_memory() instead. */
70
- void __msan_poison(const volatile void *a, size_t size);
71
-
72
- /* Returns the offset of the first (at least partially) poisoned byte in the
73
- memory range, or -1 if the whole range is good. */
74
- intptr_t __msan_test_shadow(const volatile void *x, size_t size);
75
- #endif
76
-
77
- /* detects whether we are being compiled under asan */
78
- #if defined (__has_feature)
79
- # if __has_feature(address_sanitizer)
80
- # define ADDRESS_SANITIZER 1
81
- # endif
82
- #elif defined(__SANITIZE_ADDRESS__)
83
- # define ADDRESS_SANITIZER 1
84
- #endif
85
-
86
- #if defined (ADDRESS_SANITIZER)
87
- /* Not all platforms that support asan provide sanitizers/asan_interface.h.
88
- * We therefore declare the functions we need ourselves, rather than trying to
89
- * include the header file... */
90
-
91
- /**
92
- * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
93
- *
94
- * This memory must be previously allocated by your program. Instrumented
95
- * code is forbidden from accessing addresses in this region until it is
96
- * unpoisoned. This function is not guaranteed to poison the entire region -
97
- * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
98
- * alignment restrictions.
99
- *
100
- * \note This function is not thread-safe because no two threads can poison or
101
- * unpoison memory in the same memory region simultaneously.
102
- *
103
- * \param addr Start of memory region.
104
- * \param size Size of memory region. */
105
- void __asan_poison_memory_region(void const volatile *addr, size_t size);
106
-
107
- /**
108
- * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
109
- *
110
- * This memory must be previously allocated by your program. Accessing
111
- * addresses in this region is allowed until this region is poisoned again.
112
- * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
113
- * to ASan alignment restrictions.
114
- *
115
- * \note This function is not thread-safe because no two threads can
116
- * poison or unpoison memory in the same memory region simultaneously.
117
- *
118
- * \param addr Start of memory region.
119
- * \param size Size of memory region. */
120
- void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
121
- #endif
122
-
123
-
124
44
  /*-**************************************************************
125
45
  * Basic Types
126
46
  *****************************************************************/
127
47
  #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
128
- # include <stdint.h>
48
+ # if defined(_AIX)
49
+ # include <inttypes.h>
50
+ # else
51
+ # include <stdint.h> /* intptr_t */
52
+ # endif
129
53
  typedef uint8_t BYTE;
54
+ typedef uint8_t U8;
55
+ typedef int8_t S8;
130
56
  typedef uint16_t U16;
131
57
  typedef int16_t S16;
132
58
  typedef uint32_t U32;
@@ -139,6 +65,8 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
139
65
  # error "this implementation requires char to be exactly 8-bit type"
140
66
  #endif
141
67
  typedef unsigned char BYTE;
68
+ typedef unsigned char U8;
69
+ typedef signed char S8;
142
70
  #if USHRT_MAX != 65535
143
71
  # error "this implementation requires short to be exactly 16-bit type"
144
72
  #endif
@@ -157,7 +85,53 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
157
85
 
158
86
 
159
87
  /*-**************************************************************
160
- * Memory I/O
88
+ * Memory I/O API
89
+ *****************************************************************/
90
+ /*=== Static platform detection ===*/
91
+ MEM_STATIC unsigned MEM_32bits(void);
92
+ MEM_STATIC unsigned MEM_64bits(void);
93
+ MEM_STATIC unsigned MEM_isLittleEndian(void);
94
+
95
+ /*=== Native unaligned read/write ===*/
96
+ MEM_STATIC U16 MEM_read16(const void* memPtr);
97
+ MEM_STATIC U32 MEM_read32(const void* memPtr);
98
+ MEM_STATIC U64 MEM_read64(const void* memPtr);
99
+ MEM_STATIC size_t MEM_readST(const void* memPtr);
100
+
101
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value);
102
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value);
103
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value);
104
+
105
+ /*=== Little endian unaligned read/write ===*/
106
+ MEM_STATIC U16 MEM_readLE16(const void* memPtr);
107
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr);
108
+ MEM_STATIC U32 MEM_readLE32(const void* memPtr);
109
+ MEM_STATIC U64 MEM_readLE64(const void* memPtr);
110
+ MEM_STATIC size_t MEM_readLEST(const void* memPtr);
111
+
112
+ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val);
113
+ MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val);
114
+ MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32);
115
+ MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64);
116
+ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val);
117
+
118
+ /*=== Big endian unaligned read/write ===*/
119
+ MEM_STATIC U32 MEM_readBE32(const void* memPtr);
120
+ MEM_STATIC U64 MEM_readBE64(const void* memPtr);
121
+ MEM_STATIC size_t MEM_readBEST(const void* memPtr);
122
+
123
+ MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32);
124
+ MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64);
125
+ MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val);
126
+
127
+ /*=== Byteswap ===*/
128
+ MEM_STATIC U32 MEM_swap32(U32 in);
129
+ MEM_STATIC U64 MEM_swap64(U64 in);
130
+ MEM_STATIC size_t MEM_swapST(size_t in);
131
+
132
+
133
+ /*-**************************************************************
134
+ * Memory I/O Implementation
161
135
  *****************************************************************/
162
136
  /* MEM_FORCE_MEMORY_ACCESS :
163
137
  * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
@@ -173,9 +147,7 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
173
147
  * Prefer these methods in priority order (0 > 1 > 2)
174
148
  */
175
149
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
176
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
177
- # define MEM_FORCE_MEMORY_ACCESS 2
178
- # elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)
150
+ # if defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)
179
151
  # define MEM_FORCE_MEMORY_ACCESS 1
180
152
  # endif
181
153
  #endif
@@ -185,8 +157,22 @@ MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
185
157
 
186
158
  MEM_STATIC unsigned MEM_isLittleEndian(void)
187
159
  {
160
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
161
+ return 1;
162
+ #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
163
+ return 0;
164
+ #elif defined(__clang__) && __LITTLE_ENDIAN__
165
+ return 1;
166
+ #elif defined(__clang__) && __BIG_ENDIAN__
167
+ return 0;
168
+ #elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
169
+ return 1;
170
+ #elif defined(__DMC__) && defined(_M_IX86)
171
+ return 1;
172
+ #else
188
173
  const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
189
174
  return one.c[0];
175
+ #endif
190
176
  }
191
177
 
192
178
  #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
@@ -236,37 +222,37 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v =
236
222
 
237
223
  MEM_STATIC U16 MEM_read16(const void* memPtr)
238
224
  {
239
- U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
225
+ U16 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
240
226
  }
241
227
 
242
228
  MEM_STATIC U32 MEM_read32(const void* memPtr)
243
229
  {
244
- U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
230
+ U32 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
245
231
  }
246
232
 
247
233
  MEM_STATIC U64 MEM_read64(const void* memPtr)
248
234
  {
249
- U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
235
+ U64 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
250
236
  }
251
237
 
252
238
  MEM_STATIC size_t MEM_readST(const void* memPtr)
253
239
  {
254
- size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
240
+ size_t val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
255
241
  }
256
242
 
257
243
  MEM_STATIC void MEM_write16(void* memPtr, U16 value)
258
244
  {
259
- memcpy(memPtr, &value, sizeof(value));
245
+ ZSTD_memcpy(memPtr, &value, sizeof(value));
260
246
  }
261
247
 
262
248
  MEM_STATIC void MEM_write32(void* memPtr, U32 value)
263
249
  {
264
- memcpy(memPtr, &value, sizeof(value));
250
+ ZSTD_memcpy(memPtr, &value, sizeof(value));
265
251
  }
266
252
 
267
253
  MEM_STATIC void MEM_write64(void* memPtr, U64 value)
268
254
  {
269
- memcpy(memPtr, &value, sizeof(value));
255
+ ZSTD_memcpy(memPtr, &value, sizeof(value));
270
256
  }
271
257
 
272
258
  #endif /* MEM_FORCE_MEMORY_ACCESS */
@@ -338,7 +324,7 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
338
324
 
339
325
  MEM_STATIC U32 MEM_readLE24(const void* memPtr)
340
326
  {
341
- return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
327
+ return (U32)MEM_readLE16(memPtr) + ((U32)(((const BYTE*)memPtr)[2]) << 16);
342
328
  }
343
329
 
344
330
  MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
@@ -445,6 +431,9 @@ MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
445
431
  MEM_writeBE64(memPtr, (U64)val);
446
432
  }
447
433
 
434
+ /* code only tested on 32 and 64 bits systems */
435
+ MEM_STATIC void MEM_check(void) { DEBUG_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
436
+
448
437
 
449
438
  #if defined (__cplusplus)
450
439
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -10,9 +10,9 @@
10
10
 
11
11
 
12
12
  /* ====== Dependencies ======= */
13
- #include <stddef.h> /* size_t */
13
+ #include "zstd_deps.h" /* size_t */
14
14
  #include "debug.h" /* assert */
15
- #include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
15
+ #include "zstd_internal.h" /* ZSTD_customMalloc, ZSTD_customFree */
16
16
  #include "pool.h"
17
17
 
18
18
  /* ====== Compiler specifics ====== */
@@ -105,6 +105,10 @@ static void* POOL_thread(void* opaque) {
105
105
  assert(0); /* Unreachable */
106
106
  }
107
107
 
108
+ POOL_ctx* ZSTD_createThreadPool(size_t numThreads) {
109
+ return POOL_create (numThreads, 0);
110
+ }
111
+
108
112
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
109
113
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
110
114
  }
@@ -115,14 +119,14 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
115
119
  /* Check parameters */
116
120
  if (!numThreads) { return NULL; }
117
121
  /* Allocate the context and zero initialize */
118
- ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
122
+ ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem);
119
123
  if (!ctx) { return NULL; }
120
124
  /* Initialize the job queue.
121
125
  * It needs one extra space since one space is wasted to differentiate
122
126
  * empty and full queues.
123
127
  */
124
128
  ctx->queueSize = queueSize + 1;
125
- ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
129
+ ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem);
126
130
  ctx->queueHead = 0;
127
131
  ctx->queueTail = 0;
128
132
  ctx->numThreadsBusy = 0;
@@ -136,7 +140,7 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
136
140
  }
137
141
  ctx->shutdown = 0;
138
142
  /* Allocate space for the thread handles */
139
- ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
143
+ ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
140
144
  ctx->threadCapacity = 0;
141
145
  ctx->customMem = customMem;
142
146
  /* Check for errors */
@@ -179,12 +183,14 @@ void POOL_free(POOL_ctx *ctx) {
179
183
  ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
180
184
  ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
181
185
  ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
182
- ZSTD_free(ctx->queue, ctx->customMem);
183
- ZSTD_free(ctx->threads, ctx->customMem);
184
- ZSTD_free(ctx, ctx->customMem);
186
+ ZSTD_customFree(ctx->queue, ctx->customMem);
187
+ ZSTD_customFree(ctx->threads, ctx->customMem);
188
+ ZSTD_customFree(ctx, ctx->customMem);
185
189
  }
186
190
 
187
-
191
+ void ZSTD_freeThreadPool (ZSTD_threadPool* pool) {
192
+ POOL_free (pool);
193
+ }
188
194
 
189
195
  size_t POOL_sizeof(POOL_ctx *ctx) {
190
196
  if (ctx==NULL) return 0; /* supports sizeof NULL */
@@ -203,11 +209,11 @@ static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads)
203
209
  return 0;
204
210
  }
205
211
  /* numThreads > threadCapacity */
206
- { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
212
+ { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
207
213
  if (!threadPool) return 1;
208
214
  /* replace existing thread pool */
209
- memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
210
- ZSTD_free(ctx->threads, ctx->customMem);
215
+ ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
216
+ ZSTD_customFree(ctx->threads, ctx->customMem);
211
217
  ctx->threads = threadPool;
212
218
  /* Initialize additional threads */
213
219
  { size_t threadId;
@@ -301,7 +307,7 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
301
307
  struct POOL_ctx_s {
302
308
  int dummy;
303
309
  };
304
- static POOL_ctx g_ctx;
310
+ static POOL_ctx g_poolCtx;
305
311
 
306
312
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
307
313
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
@@ -311,11 +317,11 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customM
311
317
  (void)numThreads;
312
318
  (void)queueSize;
313
319
  (void)customMem;
314
- return &g_ctx;
320
+ return &g_poolCtx;
315
321
  }
316
322
 
317
323
  void POOL_free(POOL_ctx* ctx) {
318
- assert(!ctx || ctx == &g_ctx);
324
+ assert(!ctx || ctx == &g_poolCtx);
319
325
  (void)ctx;
320
326
  }
321
327
 
@@ -337,7 +343,7 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
337
343
 
338
344
  size_t POOL_sizeof(POOL_ctx* ctx) {
339
345
  if (ctx==NULL) return 0; /* supports sizeof NULL */
340
- assert(ctx == &g_ctx);
346
+ assert(ctx == &g_poolCtx);
341
347
  return sizeof(*ctx);
342
348
  }
343
349
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -16,7 +16,7 @@ extern "C" {
16
16
  #endif
17
17
 
18
18
 
19
- #include <stddef.h> /* size_t */
19
+ #include "zstd_deps.h"
20
20
  #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
21
21
  #include "../zstd.h"
22
22
 
@@ -0,0 +1,131 @@
1
+ /*
2
+ * Copyright (c) Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ #ifndef ZSTD_PORTABILITY_MACROS_H
12
+ #define ZSTD_PORTABILITY_MACROS_H
13
+
14
+ /**
15
+ * This header file contains macro defintions to support portability.
16
+ * This header is shared between C and ASM code, so it MUST only
17
+ * contain macro definitions. It MUST not contain any C code.
18
+ *
19
+ * This header ONLY defines macros to detect platforms/feature support.
20
+ *
21
+ */
22
+
23
+
24
+ /* compat. with non-clang compilers */
25
+ #ifndef __has_attribute
26
+ #define __has_attribute(x) 0
27
+ #endif
28
+
29
+ /* compat. with non-clang compilers */
30
+ #ifndef __has_builtin
31
+ # define __has_builtin(x) 0
32
+ #endif
33
+
34
+ /* compat. with non-clang compilers */
35
+ #ifndef __has_feature
36
+ # define __has_feature(x) 0
37
+ #endif
38
+
39
+ /* detects whether we are being compiled under msan */
40
+ #ifndef ZSTD_MEMORY_SANITIZER
41
+ # if __has_feature(memory_sanitizer)
42
+ # define ZSTD_MEMORY_SANITIZER 1
43
+ # else
44
+ # define ZSTD_MEMORY_SANITIZER 0
45
+ # endif
46
+ #endif
47
+
48
+ /* detects whether we are being compiled under asan */
49
+ #ifndef ZSTD_ADDRESS_SANITIZER
50
+ # if __has_feature(address_sanitizer)
51
+ # define ZSTD_ADDRESS_SANITIZER 1
52
+ # elif defined(__SANITIZE_ADDRESS__)
53
+ # define ZSTD_ADDRESS_SANITIZER 1
54
+ # else
55
+ # define ZSTD_ADDRESS_SANITIZER 0
56
+ # endif
57
+ #endif
58
+
59
+ /* detects whether we are being compiled under dfsan */
60
+ #ifndef ZSTD_DATAFLOW_SANITIZER
61
+ # if __has_feature(dataflow_sanitizer)
62
+ # define ZSTD_DATAFLOW_SANITIZER 1
63
+ # else
64
+ # define ZSTD_DATAFLOW_SANITIZER 0
65
+ # endif
66
+ #endif
67
+
68
+
69
+ /* Enable runtime BMI2 dispatch based on the CPU.
70
+ * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
71
+ */
72
+ #ifndef DYNAMIC_BMI2
73
+ #if ((defined(__clang__) && __has_attribute(__target__)) \
74
+ || (defined(__GNUC__) \
75
+ && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
76
+ && (defined(__x86_64__) || defined(_M_X64)) \
77
+ && !defined(__BMI2__)
78
+ # define DYNAMIC_BMI2 1
79
+ #else
80
+ # define DYNAMIC_BMI2 0
81
+ #endif
82
+ #endif
83
+
84
+ /**
85
+ * Only enable assembly for GNUC comptabile compilers,
86
+ * because other platforms may not support GAS assembly syntax.
87
+ *
88
+ * Only enable assembly for Linux / MacOS, other platforms may
89
+ * work, but they haven't been tested. This could likely be
90
+ * extended to BSD systems.
91
+ *
92
+ * Disable assembly when MSAN is enabled, because MSAN requires
93
+ * 100% of code to be instrumented to work.
94
+ */
95
+ #if defined(__GNUC__)
96
+ # if defined(__linux__) || defined(__linux) || defined(__APPLE__)
97
+ # if ZSTD_MEMORY_SANITIZER
98
+ # define ZSTD_ASM_SUPPORTED 0
99
+ # elif ZSTD_DATAFLOW_SANITIZER
100
+ # define ZSTD_ASM_SUPPORTED 0
101
+ # else
102
+ # define ZSTD_ASM_SUPPORTED 1
103
+ # endif
104
+ # else
105
+ # define ZSTD_ASM_SUPPORTED 0
106
+ # endif
107
+ #else
108
+ # define ZSTD_ASM_SUPPORTED 0
109
+ #endif
110
+
111
+ /**
112
+ * Determines whether we should enable assembly for x86-64
113
+ * with BMI2.
114
+ *
115
+ * Enable if all of the following conditions hold:
116
+ * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM
117
+ * - Assembly is supported
118
+ * - We are compiling for x86-64 and either:
119
+ * - DYNAMIC_BMI2 is enabled
120
+ * - BMI2 is supported at compile time
121
+ */
122
+ #if !defined(ZSTD_DISABLE_ASM) && \
123
+ ZSTD_ASM_SUPPORTED && \
124
+ defined(__x86_64__) && \
125
+ (DYNAMIC_BMI2 || defined(__BMI2__))
126
+ # define ZSTD_ENABLE_ASM_X86_64_BMI2 1
127
+ #else
128
+ # define ZSTD_ENABLE_ASM_X86_64_BMI2 0
129
+ #endif
130
+
131
+ #endif /* ZSTD_PORTABILITY_MACROS_H */
@@ -78,11 +78,12 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
78
78
 
79
79
  #if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
80
80
 
81
- #include <stdlib.h>
81
+ #define ZSTD_DEPS_NEED_MALLOC
82
+ #include "zstd_deps.h"
82
83
 
83
84
  int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
84
85
  {
85
- *mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
86
+ *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
86
87
  if (!*mutex)
87
88
  return 1;
88
89
  return pthread_mutex_init(*mutex, attr);
@@ -94,14 +95,14 @@ int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
94
95
  return 0;
95
96
  {
96
97
  int const ret = pthread_mutex_destroy(*mutex);
97
- free(*mutex);
98
+ ZSTD_free(*mutex);
98
99
  return ret;
99
100
  }
100
101
  }
101
102
 
102
103
  int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
103
104
  {
104
- *cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
105
+ *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
105
106
  if (!*cond)
106
107
  return 1;
107
108
  return pthread_cond_init(*cond, attr);
@@ -113,7 +114,7 @@ int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
113
114
  return 0;
114
115
  {
115
116
  int const ret = pthread_cond_destroy(*cond);
116
- free(*cond);
117
+ ZSTD_free(*cond);
117
118
  return ret;
118
119
  }
119
120
  }