zstdlib 0.6.0-x64-mingw32 → 0.9.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +20 -0
  3. data/README.md +7 -1
  4. data/Rakefile +38 -8
  5. data/ext/{zstdlib → zstdlib_c}/extconf.rb +10 -5
  6. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.2/zstdlib.c +2 -2
  7. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.3/zstdlib.c +2 -2
  8. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.4/zstdlib.c +2 -2
  9. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.5/zstdlib.c +2 -2
  10. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.6/zstdlib.c +2 -2
  11. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.7/zstdlib.c +2 -2
  12. data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
  13. data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
  14. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/adler32.c +0 -0
  15. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/compress.c +0 -0
  16. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/crc32.c +0 -0
  17. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/crc32.h +0 -0
  18. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/deflate.c +0 -0
  19. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/deflate.h +0 -0
  20. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzclose.c +0 -0
  21. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzguts.h +0 -0
  22. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzlib.c +0 -0
  23. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzread.c +0 -0
  24. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/gzwrite.c +0 -0
  25. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/infback.c +0 -0
  26. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffast.c +0 -0
  27. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffast.h +0 -0
  28. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inffixed.h +0 -0
  29. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inflate.c +0 -0
  30. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inflate.h +0 -0
  31. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inftrees.c +0 -0
  32. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/inftrees.h +0 -0
  33. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/trees.c +0 -0
  34. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/trees.h +0 -0
  35. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/uncompr.c +0 -0
  36. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zconf.h +0 -0
  37. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zlib.h +0 -0
  38. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zutil.c +0 -0
  39. data/ext/{zstdlib → zstdlib_c}/zlib-1.2.11/zutil.h +0 -0
  40. data/ext/{zstdlib → zstdlib_c}/zlib.mk +0 -0
  41. data/ext/{zstdlib → zstdlib_c}/zlibwrapper/zlibwrapper.c +1 -5
  42. data/ext/{zstdlib → zstdlib_c}/zlibwrapper.mk +0 -0
  43. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/bitstream.h +75 -57
  44. data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
  45. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/cpu.h +1 -3
  46. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.c +24 -0
  47. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/debug.h +22 -49
  48. data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
  49. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/error_private.c +3 -1
  50. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
  51. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/fse.h +51 -42
  52. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/fse_decompress.c +149 -57
  53. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/huf.h +60 -54
  54. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/mem.h +87 -98
  55. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/pool.c +34 -23
  56. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/pool.h +5 -5
  57. data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
  58. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/threading.c +10 -8
  59. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/threading.h +4 -3
  60. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
  61. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
  62. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/common/zstd_common.c +10 -10
  63. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
  64. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_internal.h +493 -0
  65. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
  66. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
  67. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/fse_compress.c +105 -85
  68. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.c +41 -63
  69. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.h +13 -33
  70. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
  71. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress.c +6327 -0
  72. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_internal.h +537 -82
  73. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.c +21 -16
  74. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.h +4 -2
  75. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.c +61 -34
  76. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.h +10 -3
  77. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.c +573 -0
  78. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.h +32 -0
  79. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_cwksp.h +236 -95
  80. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.c +321 -143
  81. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.h +2 -2
  82. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.c +328 -137
  83. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.h +2 -2
  84. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
  85. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
  86. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.c +336 -209
  87. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.h +15 -3
  88. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
  89. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.c +439 -239
  90. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.h +1 -1
  91. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/compress/zstdmt_compress.c +205 -462
  92. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
  93. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress.c +1889 -0
  94. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
  95. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.c +20 -16
  96. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.h +3 -3
  97. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress.c +691 -230
  98. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.c +2072 -0
  99. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_block.h +16 -7
  100. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_internal.h +71 -10
  101. data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
  102. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/lib/zstd.h +760 -234
  103. data/ext/{zstdlib/zstd-1.4.4/lib/common → zstdlib_c/zstd-1.5.2/lib}/zstd_errors.h +3 -1
  104. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzclose.c +0 -0
  105. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzcompatibility.h +1 -1
  106. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzguts.h +0 -0
  107. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzlib.c +0 -0
  108. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzread.c +0 -0
  109. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzwrite.c +0 -0
  110. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.c +133 -44
  111. data/ext/{zstdlib/zstd-1.4.4 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.h +1 -1
  112. data/ext/zstdlib_c/zstd.mk +15 -0
  113. data/lib/2.4/zstdlib_c.so +0 -0
  114. data/lib/2.5/zstdlib_c.so +0 -0
  115. data/lib/2.6/zstdlib_c.so +0 -0
  116. data/lib/2.7/zstdlib_c.so +0 -0
  117. data/lib/3.0/zstdlib_c.so +0 -0
  118. data/lib/zstdlib.rb +2 -2
  119. metadata +124 -114
  120. data/ext/zstdlib/zstd-1.4.4/lib/common/compiler.h +0 -159
  121. data/ext/zstdlib/zstd-1.4.4/lib/common/debug.c +0 -44
  122. data/ext/zstdlib/zstd-1.4.4/lib/common/entropy_common.c +0 -236
  123. data/ext/zstdlib/zstd-1.4.4/lib/common/error_private.h +0 -76
  124. data/ext/zstdlib/zstd-1.4.4/lib/common/xxhash.c +0 -882
  125. data/ext/zstdlib/zstd-1.4.4/lib/common/xxhash.h +0 -305
  126. data/ext/zstdlib/zstd-1.4.4/lib/common/zstd_internal.h +0 -350
  127. data/ext/zstdlib/zstd-1.4.4/lib/compress/huf_compress.c +0 -798
  128. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_compress.c +0 -4103
  129. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_lazy.c +0 -1115
  130. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstd_lazy.h +0 -67
  131. data/ext/zstdlib/zstd-1.4.4/lib/compress/zstdmt_compress.h +0 -192
  132. data/ext/zstdlib/zstd-1.4.4/lib/decompress/huf_decompress.c +0 -1234
  133. data/ext/zstdlib/zstd-1.4.4/lib/decompress/zstd_decompress_block.c +0 -1323
  134. data/ext/zstdlib/zstd.mk +0 -14
  135. data/lib/2.2/zstdlib.so +0 -0
  136. data/lib/2.3/zstdlib.so +0 -0
  137. data/lib/2.4/zstdlib.so +0 -0
  138. data/lib/2.5/zstdlib.so +0 -0
  139. data/lib/2.6/zstdlib.so +0 -0
  140. data/lib/2.7/zstdlib.so +0 -0
@@ -1,35 +1,15 @@
1
1
  /* ******************************************************************
2
- huff0 huffman codec,
3
- part of Finite State Entropy library
4
- Copyright (C) 2013-present, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
2
+ * huff0 huffman codec,
3
+ * part of Finite State Entropy library
4
+ * Copyright (c) Yann Collet, Facebook, Inc.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
33
13
  ****************************************************************** */
34
14
 
35
15
  #if defined (__cplusplus)
@@ -40,7 +20,7 @@ extern "C" {
40
20
  #define HUF_H_298734234
41
21
 
42
22
  /* *** Dependencies *** */
43
- #include <stddef.h> /* size_t */
23
+ #include "zstd_deps.h" /* size_t */
44
24
 
45
25
 
46
26
  /* *** library symbols visibility *** */
@@ -109,9 +89,9 @@ HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
109
89
 
110
90
  /** HUF_compress4X_wksp() :
111
91
  * Same as HUF_compress2(), but uses externally allocated `workSpace`.
112
- * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
113
- #define HUF_WORKSPACE_SIZE (6 << 10)
114
- #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
92
+ * `workspace` must be at least as large as HUF_WORKSPACE_SIZE */
93
+ #define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
94
+ #define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
115
95
  HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
116
96
  const void* src, size_t srcSize,
117
97
  unsigned maxSymbolValue, unsigned tableLog,
@@ -131,14 +111,16 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
131
111
 
132
112
  /* *** Dependencies *** */
133
113
  #include "mem.h" /* U32 */
114
+ #define FSE_STATIC_LINKING_ONLY
115
+ #include "fse.h"
134
116
 
135
117
 
136
118
  /* *** Constants *** */
137
- #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
119
+ #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */
138
120
  #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
139
121
  #define HUF_SYMBOLVALUE_MAX 255
140
122
 
141
- #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
123
+ #define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
142
124
  #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
143
125
  # error "HUF_TABLELOG_MAX is too large !"
144
126
  #endif
@@ -153,12 +135,12 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
153
135
  #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
154
136
 
155
137
  /* static allocation of HUF's Compression Table */
156
- #define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */
157
- #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
138
+ /* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */
139
+ typedef size_t HUF_CElt; /* consider it an incomplete type */
140
+ #define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */
141
+ #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))
158
142
  #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
159
- U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \
160
- void* name##hv = &(name##hb); \
161
- HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
143
+ HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */
162
144
 
163
145
  /* static allocation of HUF's DTable */
164
146
  typedef U32 HUF_DTable;
@@ -204,10 +186,13 @@ size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
204
186
  * or to save and regenerate 'CTable' using external methods.
205
187
  */
206
188
  unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
207
- typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
208
189
  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 */
209
190
  size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
191
+ size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
210
192
  size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
193
+ size_t HUF_compress4X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);
194
+ size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
195
+ int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
211
196
 
212
197
  typedef enum {
213
198
  HUF_repeat_none, /**< Cannot use the previous table */
@@ -218,12 +203,13 @@ typedef enum {
218
203
  * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
219
204
  * If it uses hufTable it does not modify hufTable or repeat.
220
205
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
221
- * If preferRepeat then the old table will always be used if valid. */
206
+ * If preferRepeat then the old table will always be used if valid.
207
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
222
208
  size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
223
209
  const void* src, size_t srcSize,
224
210
  unsigned maxSymbolValue, unsigned tableLog,
225
211
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
226
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
212
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
227
213
 
228
214
  /** HUF_buildCTable_wksp() :
229
215
  * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
@@ -244,15 +230,27 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
244
230
  U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
245
231
  const void* src, size_t srcSize);
246
232
 
233
+ /*! HUF_readStats_wksp() :
234
+ * Same as HUF_readStats() but takes an external workspace which must be
235
+ * 4-byte aligned and its size must be >= HUF_READ_STATS_WORKSPACE_SIZE.
236
+ * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
237
+ */
238
+ #define HUF_READ_STATS_WORKSPACE_SIZE_U32 FSE_DECOMPRESS_WKSP_SIZE_U32(6, HUF_TABLELOG_MAX-1)
239
+ #define HUF_READ_STATS_WORKSPACE_SIZE (HUF_READ_STATS_WORKSPACE_SIZE_U32 * sizeof(unsigned))
240
+ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,
241
+ U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
242
+ const void* src, size_t srcSize,
243
+ void* workspace, size_t wkspSize,
244
+ int bmi2);
245
+
247
246
  /** HUF_readCTable() :
248
247
  * Loading a CTable saved with HUF_writeCTable() */
249
- size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
248
+ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
250
249
 
251
- /** HUF_getNbBits() :
250
+ /** HUF_getNbBitsFromCTable() :
252
251
  * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
253
- * Note 1 : is not inlined, as HUF_CElt definition is private
254
- * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
255
- U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
252
+ * Note 1 : is not inlined, as HUF_CElt definition is private */
253
+ U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
256
254
 
257
255
  /*
258
256
  * HUF_decompress() does the following:
@@ -278,7 +276,7 @@ U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
278
276
  * a required workspace size greater than that specified in the following
279
277
  * macro.
280
278
  */
281
- #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
279
+ #define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))
282
280
  #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
283
281
 
284
282
  #ifndef HUF_FORCE_DECOMPRESS_X2
@@ -304,18 +302,20 @@ size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* c
304
302
  /* ====================== */
305
303
 
306
304
  size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
307
- 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 */
305
+ 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_U64 U64 */
308
306
  size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
307
+ size_t HUF_compress1X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);
309
308
  /** HUF_compress1X_repeat() :
310
309
  * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
311
310
  * If it uses hufTable it does not modify hufTable or repeat.
312
311
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
313
- * If preferRepeat then the old table will always be used if valid. */
312
+ * If preferRepeat then the old table will always be used if valid.
313
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
314
314
  size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
315
315
  const void* src, size_t srcSize,
316
316
  unsigned maxSymbolValue, unsigned tableLog,
317
317
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
318
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
318
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
319
319
 
320
320
  size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
321
321
  #ifndef HUF_FORCE_DECOMPRESS_X1
@@ -350,6 +350,12 @@ size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstS
350
350
  #endif
351
351
  size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
352
352
  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);
353
+ #ifndef HUF_FORCE_DECOMPRESS_X2
354
+ size_t HUF_readDTableX1_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);
355
+ #endif
356
+ #ifndef HUF_FORCE_DECOMPRESS_X1
357
+ size_t HUF_readDTableX2_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);
358
+ #endif
353
359
 
354
360
  #endif /* HUF_STATIC_LINKING_ONLY */
355
361
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -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-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -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 ====== */
@@ -86,7 +86,7 @@ static void* POOL_thread(void* opaque) {
86
86
  { POOL_job const job = ctx->queue[ctx->queueHead];
87
87
  ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
88
88
  ctx->numThreadsBusy++;
89
- ctx->queueEmpty = ctx->queueHead == ctx->queueTail;
89
+ ctx->queueEmpty = (ctx->queueHead == ctx->queueTail);
90
90
  /* Unlock the mutex, signal a pusher, and run the job */
91
91
  ZSTD_pthread_cond_signal(&ctx->queuePushCond);
92
92
  ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
@@ -105,24 +105,30 @@ static void* POOL_thread(void* opaque) {
105
105
  assert(0); /* Unreachable */
106
106
  }
107
107
 
108
+ /* ZSTD_createThreadPool() : public access point */
109
+ POOL_ctx* ZSTD_createThreadPool(size_t numThreads) {
110
+ return POOL_create (numThreads, 0);
111
+ }
112
+
108
113
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
109
114
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
110
115
  }
111
116
 
112
117
  POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
113
- ZSTD_customMem customMem) {
118
+ ZSTD_customMem customMem)
119
+ {
114
120
  POOL_ctx* ctx;
115
121
  /* Check parameters */
116
122
  if (!numThreads) { return NULL; }
117
123
  /* Allocate the context and zero initialize */
118
- ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
124
+ ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem);
119
125
  if (!ctx) { return NULL; }
120
126
  /* Initialize the job queue.
121
127
  * It needs one extra space since one space is wasted to differentiate
122
128
  * empty and full queues.
123
129
  */
124
130
  ctx->queueSize = queueSize + 1;
125
- ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
131
+ ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem);
126
132
  ctx->queueHead = 0;
127
133
  ctx->queueTail = 0;
128
134
  ctx->numThreadsBusy = 0;
@@ -136,7 +142,7 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
136
142
  }
137
143
  ctx->shutdown = 0;
138
144
  /* Allocate space for the thread handles */
139
- ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
145
+ ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
140
146
  ctx->threadCapacity = 0;
141
147
  ctx->customMem = customMem;
142
148
  /* Check for errors */
@@ -179,14 +185,16 @@ void POOL_free(POOL_ctx *ctx) {
179
185
  ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
180
186
  ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
181
187
  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);
188
+ ZSTD_customFree(ctx->queue, ctx->customMem);
189
+ ZSTD_customFree(ctx->threads, ctx->customMem);
190
+ ZSTD_customFree(ctx, ctx->customMem);
185
191
  }
186
192
 
193
+ void ZSTD_freeThreadPool (ZSTD_threadPool* pool) {
194
+ POOL_free (pool);
195
+ }
187
196
 
188
-
189
- size_t POOL_sizeof(POOL_ctx *ctx) {
197
+ size_t POOL_sizeof(const POOL_ctx* ctx) {
190
198
  if (ctx==NULL) return 0; /* supports sizeof NULL */
191
199
  return sizeof(*ctx)
192
200
  + ctx->queueSize * sizeof(POOL_job)
@@ -203,11 +211,11 @@ static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads)
203
211
  return 0;
204
212
  }
205
213
  /* numThreads > threadCapacity */
206
- { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
214
+ { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
207
215
  if (!threadPool) return 1;
208
216
  /* replace existing thread pool */
209
- memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
210
- ZSTD_free(ctx->threads, ctx->customMem);
217
+ ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
218
+ ZSTD_customFree(ctx->threads, ctx->customMem);
211
219
  ctx->threads = threadPool;
212
220
  /* Initialize additional threads */
213
221
  { size_t threadId;
@@ -251,7 +259,8 @@ static int isQueueFull(POOL_ctx const* ctx) {
251
259
  }
252
260
 
253
261
 
254
- static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque)
262
+ static void
263
+ POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque)
255
264
  {
256
265
  POOL_job const job = {function, opaque};
257
266
  assert(ctx != NULL);
@@ -301,21 +310,23 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
301
310
  struct POOL_ctx_s {
302
311
  int dummy;
303
312
  };
304
- static POOL_ctx g_ctx;
313
+ static POOL_ctx g_poolCtx;
305
314
 
306
315
  POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
307
316
  return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
308
317
  }
309
318
 
310
- POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
319
+ POOL_ctx*
320
+ POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem)
321
+ {
311
322
  (void)numThreads;
312
323
  (void)queueSize;
313
324
  (void)customMem;
314
- return &g_ctx;
325
+ return &g_poolCtx;
315
326
  }
316
327
 
317
328
  void POOL_free(POOL_ctx* ctx) {
318
- assert(!ctx || ctx == &g_ctx);
329
+ assert(!ctx || ctx == &g_poolCtx);
319
330
  (void)ctx;
320
331
  }
321
332
 
@@ -335,9 +346,9 @@ int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
335
346
  return 1;
336
347
  }
337
348
 
338
- size_t POOL_sizeof(POOL_ctx* ctx) {
349
+ size_t POOL_sizeof(const POOL_ctx* ctx) {
339
350
  if (ctx==NULL) return 0; /* supports sizeof NULL */
340
- assert(ctx == &g_ctx);
351
+ assert(ctx == &g_poolCtx);
341
352
  return sizeof(*ctx);
342
353
  }
343
354