extzstd 0.0.3.CONCEPT → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, 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_COMPRESS_ADVANCED_H
12
+ #define ZSTD_COMPRESS_ADVANCED_H
13
+
14
+ /*-*************************************
15
+ * Dependencies
16
+ ***************************************/
17
+
18
+ #include "../zstd.h" /* ZSTD_CCtx */
19
+
20
+ /*-*************************************
21
+ * Target Compressed Block Size
22
+ ***************************************/
23
+
24
+ /* ZSTD_compressSuperBlock() :
25
+ * Used to compress a super block when targetCBlockSize is being used.
26
+ * The given block will be compressed into multiple sub blocks that are around targetCBlockSize. */
27
+ size_t ZSTD_compressSuperBlock(ZSTD_CCtx* zc,
28
+ void* dst, size_t dstCapacity,
29
+ void const* src, size_t srcSize,
30
+ unsigned lastBlock);
31
+
32
+ #endif /* ZSTD_COMPRESS_ADVANCED_H */
@@ -0,0 +1,525 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, 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_CWKSP_H
12
+ #define ZSTD_CWKSP_H
13
+
14
+ /*-*************************************
15
+ * Dependencies
16
+ ***************************************/
17
+ #include "../common/zstd_internal.h"
18
+
19
+ #if defined (__cplusplus)
20
+ extern "C" {
21
+ #endif
22
+
23
+ /*-*************************************
24
+ * Constants
25
+ ***************************************/
26
+
27
+ /* Since the workspace is effectively its own little malloc implementation /
28
+ * arena, when we run under ASAN, we should similarly insert redzones between
29
+ * each internal element of the workspace, so ASAN will catch overruns that
30
+ * reach outside an object but that stay inside the workspace.
31
+ *
32
+ * This defines the size of that redzone.
33
+ */
34
+ #ifndef ZSTD_CWKSP_ASAN_REDZONE_SIZE
35
+ #define ZSTD_CWKSP_ASAN_REDZONE_SIZE 128
36
+ #endif
37
+
38
+ /*-*************************************
39
+ * Structures
40
+ ***************************************/
41
+ typedef enum {
42
+ ZSTD_cwksp_alloc_objects,
43
+ ZSTD_cwksp_alloc_buffers,
44
+ ZSTD_cwksp_alloc_aligned
45
+ } ZSTD_cwksp_alloc_phase_e;
46
+
47
+ /**
48
+ * Zstd fits all its internal datastructures into a single continuous buffer,
49
+ * so that it only needs to perform a single OS allocation (or so that a buffer
50
+ * can be provided to it and it can perform no allocations at all). This buffer
51
+ * is called the workspace.
52
+ *
53
+ * Several optimizations complicate that process of allocating memory ranges
54
+ * from this workspace for each internal datastructure:
55
+ *
56
+ * - These different internal datastructures have different setup requirements:
57
+ *
58
+ * - The static objects need to be cleared once and can then be trivially
59
+ * reused for each compression.
60
+ *
61
+ * - Various buffers don't need to be initialized at all--they are always
62
+ * written into before they're read.
63
+ *
64
+ * - The matchstate tables have a unique requirement that they don't need
65
+ * their memory to be totally cleared, but they do need the memory to have
66
+ * some bound, i.e., a guarantee that all values in the memory they've been
67
+ * allocated is less than some maximum value (which is the starting value
68
+ * for the indices that they will then use for compression). When this
69
+ * guarantee is provided to them, they can use the memory without any setup
70
+ * work. When it can't, they have to clear the area.
71
+ *
72
+ * - These buffers also have different alignment requirements.
73
+ *
74
+ * - We would like to reuse the objects in the workspace for multiple
75
+ * compressions without having to perform any expensive reallocation or
76
+ * reinitialization work.
77
+ *
78
+ * - We would like to be able to efficiently reuse the workspace across
79
+ * multiple compressions **even when the compression parameters change** and
80
+ * we need to resize some of the objects (where possible).
81
+ *
82
+ * To attempt to manage this buffer, given these constraints, the ZSTD_cwksp
83
+ * abstraction was created. It works as follows:
84
+ *
85
+ * Workspace Layout:
86
+ *
87
+ * [ ... workspace ... ]
88
+ * [objects][tables ... ->] free space [<- ... aligned][<- ... buffers]
89
+ *
90
+ * The various objects that live in the workspace are divided into the
91
+ * following categories, and are allocated separately:
92
+ *
93
+ * - Static objects: this is optionally the enclosing ZSTD_CCtx or ZSTD_CDict,
94
+ * so that literally everything fits in a single buffer. Note: if present,
95
+ * this must be the first object in the workspace, since ZSTD_free{CCtx,
96
+ * CDict}() rely on a pointer comparison to see whether one or two frees are
97
+ * required.
98
+ *
99
+ * - Fixed size objects: these are fixed-size, fixed-count objects that are
100
+ * nonetheless "dynamically" allocated in the workspace so that we can
101
+ * control how they're initialized separately from the broader ZSTD_CCtx.
102
+ * Examples:
103
+ * - Entropy Workspace
104
+ * - 2 x ZSTD_compressedBlockState_t
105
+ * - CDict dictionary contents
106
+ *
107
+ * - Tables: these are any of several different datastructures (hash tables,
108
+ * chain tables, binary trees) that all respect a common format: they are
109
+ * uint32_t arrays, all of whose values are between 0 and (nextSrc - base).
110
+ * Their sizes depend on the cparams.
111
+ *
112
+ * - Aligned: these buffers are used for various purposes that require 4 byte
113
+ * alignment, but don't require any initialization before they're used.
114
+ *
115
+ * - Buffers: these buffers are used for various purposes that don't require
116
+ * any alignment or initialization before they're used. This means they can
117
+ * be moved around at no cost for a new compression.
118
+ *
119
+ * Allocating Memory:
120
+ *
121
+ * The various types of objects must be allocated in order, so they can be
122
+ * correctly packed into the workspace buffer. That order is:
123
+ *
124
+ * 1. Objects
125
+ * 2. Buffers
126
+ * 3. Aligned
127
+ * 4. Tables
128
+ *
129
+ * Attempts to reserve objects of different types out of order will fail.
130
+ */
131
+ typedef struct {
132
+ void* workspace;
133
+ void* workspaceEnd;
134
+
135
+ void* objectEnd;
136
+ void* tableEnd;
137
+ void* tableValidEnd;
138
+ void* allocStart;
139
+
140
+ int allocFailed;
141
+ int workspaceOversizedDuration;
142
+ ZSTD_cwksp_alloc_phase_e phase;
143
+ } ZSTD_cwksp;
144
+
145
+ /*-*************************************
146
+ * Functions
147
+ ***************************************/
148
+
149
+ MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp* ws);
150
+
151
+ MEM_STATIC void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp* ws) {
152
+ (void)ws;
153
+ assert(ws->workspace <= ws->objectEnd);
154
+ assert(ws->objectEnd <= ws->tableEnd);
155
+ assert(ws->objectEnd <= ws->tableValidEnd);
156
+ assert(ws->tableEnd <= ws->allocStart);
157
+ assert(ws->tableValidEnd <= ws->allocStart);
158
+ assert(ws->allocStart <= ws->workspaceEnd);
159
+ }
160
+
161
+ /**
162
+ * Align must be a power of 2.
163
+ */
164
+ MEM_STATIC size_t ZSTD_cwksp_align(size_t size, size_t const align) {
165
+ size_t const mask = align - 1;
166
+ assert((align & mask) == 0);
167
+ return (size + mask) & ~mask;
168
+ }
169
+
170
+ /**
171
+ * Use this to determine how much space in the workspace we will consume to
172
+ * allocate this object. (Normally it should be exactly the size of the object,
173
+ * but under special conditions, like ASAN, where we pad each object, it might
174
+ * be larger.)
175
+ *
176
+ * Since tables aren't currently redzoned, you don't need to call through this
177
+ * to figure out how much space you need for the matchState tables. Everything
178
+ * else is though.
179
+ */
180
+ MEM_STATIC size_t ZSTD_cwksp_alloc_size(size_t size) {
181
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
182
+ return size + 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
183
+ #else
184
+ return size;
185
+ #endif
186
+ }
187
+
188
+ MEM_STATIC void ZSTD_cwksp_internal_advance_phase(
189
+ ZSTD_cwksp* ws, ZSTD_cwksp_alloc_phase_e phase) {
190
+ assert(phase >= ws->phase);
191
+ if (phase > ws->phase) {
192
+ if (ws->phase < ZSTD_cwksp_alloc_buffers &&
193
+ phase >= ZSTD_cwksp_alloc_buffers) {
194
+ ws->tableValidEnd = ws->objectEnd;
195
+ }
196
+ if (ws->phase < ZSTD_cwksp_alloc_aligned &&
197
+ phase >= ZSTD_cwksp_alloc_aligned) {
198
+ /* If unaligned allocations down from a too-large top have left us
199
+ * unaligned, we need to realign our alloc ptr. Technically, this
200
+ * can consume space that is unaccounted for in the neededSpace
201
+ * calculation. However, I believe this can only happen when the
202
+ * workspace is too large, and specifically when it is too large
203
+ * by a larger margin than the space that will be consumed. */
204
+ /* TODO: cleaner, compiler warning friendly way to do this??? */
205
+ ws->allocStart = (BYTE*)ws->allocStart - ((size_t)ws->allocStart & (sizeof(U32)-1));
206
+ if (ws->allocStart < ws->tableValidEnd) {
207
+ ws->tableValidEnd = ws->allocStart;
208
+ }
209
+ }
210
+ ws->phase = phase;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Returns whether this object/buffer/etc was allocated in this workspace.
216
+ */
217
+ MEM_STATIC int ZSTD_cwksp_owns_buffer(const ZSTD_cwksp* ws, const void* ptr) {
218
+ return (ptr != NULL) && (ws->workspace <= ptr) && (ptr <= ws->workspaceEnd);
219
+ }
220
+
221
+ /**
222
+ * Internal function. Do not use directly.
223
+ */
224
+ MEM_STATIC void* ZSTD_cwksp_reserve_internal(
225
+ ZSTD_cwksp* ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase) {
226
+ void* alloc;
227
+ void* bottom = ws->tableEnd;
228
+ ZSTD_cwksp_internal_advance_phase(ws, phase);
229
+ alloc = (BYTE *)ws->allocStart - bytes;
230
+
231
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
232
+ /* over-reserve space */
233
+ alloc = (BYTE *)alloc - 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
234
+ #endif
235
+
236
+ DEBUGLOG(5, "cwksp: reserving %p %zd bytes, %zd bytes remaining",
237
+ alloc, bytes, ZSTD_cwksp_available_space(ws) - bytes);
238
+ ZSTD_cwksp_assert_internal_consistency(ws);
239
+ assert(alloc >= bottom);
240
+ if (alloc < bottom) {
241
+ DEBUGLOG(4, "cwksp: alloc failed!");
242
+ ws->allocFailed = 1;
243
+ return NULL;
244
+ }
245
+ if (alloc < ws->tableValidEnd) {
246
+ ws->tableValidEnd = alloc;
247
+ }
248
+ ws->allocStart = alloc;
249
+
250
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
251
+ /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
252
+ * either size. */
253
+ alloc = (BYTE *)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
254
+ __asan_unpoison_memory_region(alloc, bytes);
255
+ #endif
256
+
257
+ return alloc;
258
+ }
259
+
260
+ /**
261
+ * Reserves and returns unaligned memory.
262
+ */
263
+ MEM_STATIC BYTE* ZSTD_cwksp_reserve_buffer(ZSTD_cwksp* ws, size_t bytes) {
264
+ return (BYTE*)ZSTD_cwksp_reserve_internal(ws, bytes, ZSTD_cwksp_alloc_buffers);
265
+ }
266
+
267
+ /**
268
+ * Reserves and returns memory sized on and aligned on sizeof(unsigned).
269
+ */
270
+ MEM_STATIC void* ZSTD_cwksp_reserve_aligned(ZSTD_cwksp* ws, size_t bytes) {
271
+ assert((bytes & (sizeof(U32)-1)) == 0);
272
+ return ZSTD_cwksp_reserve_internal(ws, ZSTD_cwksp_align(bytes, sizeof(U32)), ZSTD_cwksp_alloc_aligned);
273
+ }
274
+
275
+ /**
276
+ * Aligned on sizeof(unsigned). These buffers have the special property that
277
+ * their values remain constrained, allowing us to re-use them without
278
+ * memset()-ing them.
279
+ */
280
+ MEM_STATIC void* ZSTD_cwksp_reserve_table(ZSTD_cwksp* ws, size_t bytes) {
281
+ const ZSTD_cwksp_alloc_phase_e phase = ZSTD_cwksp_alloc_aligned;
282
+ void* alloc = ws->tableEnd;
283
+ void* end = (BYTE *)alloc + bytes;
284
+ void* top = ws->allocStart;
285
+
286
+ DEBUGLOG(5, "cwksp: reserving %p table %zd bytes, %zd bytes remaining",
287
+ alloc, bytes, ZSTD_cwksp_available_space(ws) - bytes);
288
+ assert((bytes & (sizeof(U32)-1)) == 0);
289
+ ZSTD_cwksp_internal_advance_phase(ws, phase);
290
+ ZSTD_cwksp_assert_internal_consistency(ws);
291
+ assert(end <= top);
292
+ if (end > top) {
293
+ DEBUGLOG(4, "cwksp: table alloc failed!");
294
+ ws->allocFailed = 1;
295
+ return NULL;
296
+ }
297
+ ws->tableEnd = end;
298
+
299
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
300
+ __asan_unpoison_memory_region(alloc, bytes);
301
+ #endif
302
+
303
+ return alloc;
304
+ }
305
+
306
+ /**
307
+ * Aligned on sizeof(void*).
308
+ */
309
+ MEM_STATIC void* ZSTD_cwksp_reserve_object(ZSTD_cwksp* ws, size_t bytes) {
310
+ size_t roundedBytes = ZSTD_cwksp_align(bytes, sizeof(void*));
311
+ void* alloc = ws->objectEnd;
312
+ void* end = (BYTE*)alloc + roundedBytes;
313
+
314
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
315
+ /* over-reserve space */
316
+ end = (BYTE *)end + 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
317
+ #endif
318
+
319
+ DEBUGLOG(5,
320
+ "cwksp: reserving %p object %zd bytes (rounded to %zd), %zd bytes remaining",
321
+ alloc, bytes, roundedBytes, ZSTD_cwksp_available_space(ws) - roundedBytes);
322
+ assert(((size_t)alloc & (sizeof(void*)-1)) == 0);
323
+ assert((bytes & (sizeof(void*)-1)) == 0);
324
+ ZSTD_cwksp_assert_internal_consistency(ws);
325
+ /* we must be in the first phase, no advance is possible */
326
+ if (ws->phase != ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) {
327
+ DEBUGLOG(4, "cwksp: object alloc failed!");
328
+ ws->allocFailed = 1;
329
+ return NULL;
330
+ }
331
+ ws->objectEnd = end;
332
+ ws->tableEnd = end;
333
+ ws->tableValidEnd = end;
334
+
335
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
336
+ /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
337
+ * either size. */
338
+ alloc = (BYTE *)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
339
+ __asan_unpoison_memory_region(alloc, bytes);
340
+ #endif
341
+
342
+ return alloc;
343
+ }
344
+
345
+ MEM_STATIC void ZSTD_cwksp_mark_tables_dirty(ZSTD_cwksp* ws) {
346
+ DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_dirty");
347
+
348
+ #if defined (MEMORY_SANITIZER) && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
349
+ /* To validate that the table re-use logic is sound, and that we don't
350
+ * access table space that we haven't cleaned, we re-"poison" the table
351
+ * space every time we mark it dirty. */
352
+ {
353
+ size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
354
+ assert(__msan_test_shadow(ws->objectEnd, size) == -1);
355
+ __msan_poison(ws->objectEnd, size);
356
+ }
357
+ #endif
358
+
359
+ assert(ws->tableValidEnd >= ws->objectEnd);
360
+ assert(ws->tableValidEnd <= ws->allocStart);
361
+ ws->tableValidEnd = ws->objectEnd;
362
+ ZSTD_cwksp_assert_internal_consistency(ws);
363
+ }
364
+
365
+ MEM_STATIC void ZSTD_cwksp_mark_tables_clean(ZSTD_cwksp* ws) {
366
+ DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_clean");
367
+ assert(ws->tableValidEnd >= ws->objectEnd);
368
+ assert(ws->tableValidEnd <= ws->allocStart);
369
+ if (ws->tableValidEnd < ws->tableEnd) {
370
+ ws->tableValidEnd = ws->tableEnd;
371
+ }
372
+ ZSTD_cwksp_assert_internal_consistency(ws);
373
+ }
374
+
375
+ /**
376
+ * Zero the part of the allocated tables not already marked clean.
377
+ */
378
+ MEM_STATIC void ZSTD_cwksp_clean_tables(ZSTD_cwksp* ws) {
379
+ DEBUGLOG(4, "cwksp: ZSTD_cwksp_clean_tables");
380
+ assert(ws->tableValidEnd >= ws->objectEnd);
381
+ assert(ws->tableValidEnd <= ws->allocStart);
382
+ if (ws->tableValidEnd < ws->tableEnd) {
383
+ memset(ws->tableValidEnd, 0, (BYTE*)ws->tableEnd - (BYTE*)ws->tableValidEnd);
384
+ }
385
+ ZSTD_cwksp_mark_tables_clean(ws);
386
+ }
387
+
388
+ /**
389
+ * Invalidates table allocations.
390
+ * All other allocations remain valid.
391
+ */
392
+ MEM_STATIC void ZSTD_cwksp_clear_tables(ZSTD_cwksp* ws) {
393
+ DEBUGLOG(4, "cwksp: clearing tables!");
394
+
395
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
396
+ {
397
+ size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
398
+ __asan_poison_memory_region(ws->objectEnd, size);
399
+ }
400
+ #endif
401
+
402
+ ws->tableEnd = ws->objectEnd;
403
+ ZSTD_cwksp_assert_internal_consistency(ws);
404
+ }
405
+
406
+ /**
407
+ * Invalidates all buffer, aligned, and table allocations.
408
+ * Object allocations remain valid.
409
+ */
410
+ MEM_STATIC void ZSTD_cwksp_clear(ZSTD_cwksp* ws) {
411
+ DEBUGLOG(4, "cwksp: clearing!");
412
+
413
+ #if defined (MEMORY_SANITIZER) && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
414
+ /* To validate that the context re-use logic is sound, and that we don't
415
+ * access stuff that this compression hasn't initialized, we re-"poison"
416
+ * the workspace (or at least the non-static, non-table parts of it)
417
+ * every time we start a new compression. */
418
+ {
419
+ size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->tableValidEnd;
420
+ __msan_poison(ws->tableValidEnd, size);
421
+ }
422
+ #endif
423
+
424
+ #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
425
+ {
426
+ size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->objectEnd;
427
+ __asan_poison_memory_region(ws->objectEnd, size);
428
+ }
429
+ #endif
430
+
431
+ ws->tableEnd = ws->objectEnd;
432
+ ws->allocStart = ws->workspaceEnd;
433
+ ws->allocFailed = 0;
434
+ if (ws->phase > ZSTD_cwksp_alloc_buffers) {
435
+ ws->phase = ZSTD_cwksp_alloc_buffers;
436
+ }
437
+ ZSTD_cwksp_assert_internal_consistency(ws);
438
+ }
439
+
440
+ /**
441
+ * The provided workspace takes ownership of the buffer [start, start+size).
442
+ * Any existing values in the workspace are ignored (the previously managed
443
+ * buffer, if present, must be separately freed).
444
+ */
445
+ MEM_STATIC void ZSTD_cwksp_init(ZSTD_cwksp* ws, void* start, size_t size) {
446
+ DEBUGLOG(4, "cwksp: init'ing workspace with %zd bytes", size);
447
+ assert(((size_t)start & (sizeof(void*)-1)) == 0); /* ensure correct alignment */
448
+ ws->workspace = start;
449
+ ws->workspaceEnd = (BYTE*)start + size;
450
+ ws->objectEnd = ws->workspace;
451
+ ws->tableValidEnd = ws->objectEnd;
452
+ ws->phase = ZSTD_cwksp_alloc_objects;
453
+ ZSTD_cwksp_clear(ws);
454
+ ws->workspaceOversizedDuration = 0;
455
+ ZSTD_cwksp_assert_internal_consistency(ws);
456
+ }
457
+
458
+ MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem customMem) {
459
+ void* workspace = ZSTD_malloc(size, customMem);
460
+ DEBUGLOG(4, "cwksp: creating new workspace with %zd bytes", size);
461
+ RETURN_ERROR_IF(workspace == NULL, memory_allocation, "NULL pointer!");
462
+ ZSTD_cwksp_init(ws, workspace, size);
463
+ return 0;
464
+ }
465
+
466
+ MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) {
467
+ void *ptr = ws->workspace;
468
+ DEBUGLOG(4, "cwksp: freeing workspace");
469
+ memset(ws, 0, sizeof(ZSTD_cwksp));
470
+ ZSTD_free(ptr, customMem);
471
+ }
472
+
473
+ /**
474
+ * Moves the management of a workspace from one cwksp to another. The src cwksp
475
+ * is left in an invalid state (src must be re-init()'ed before its used again).
476
+ */
477
+ MEM_STATIC void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src) {
478
+ *dst = *src;
479
+ memset(src, 0, sizeof(ZSTD_cwksp));
480
+ }
481
+
482
+ MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws) {
483
+ return (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace);
484
+ }
485
+
486
+ MEM_STATIC int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp* ws) {
487
+ return ws->allocFailed;
488
+ }
489
+
490
+ /*-*************************************
491
+ * Functions Checking Free Space
492
+ ***************************************/
493
+
494
+ MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp* ws) {
495
+ return (size_t)((BYTE*)ws->allocStart - (BYTE*)ws->tableEnd);
496
+ }
497
+
498
+ MEM_STATIC int ZSTD_cwksp_check_available(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
499
+ return ZSTD_cwksp_available_space(ws) >= additionalNeededSpace;
500
+ }
501
+
502
+ MEM_STATIC int ZSTD_cwksp_check_too_large(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
503
+ return ZSTD_cwksp_check_available(
504
+ ws, additionalNeededSpace * ZSTD_WORKSPACETOOLARGE_FACTOR);
505
+ }
506
+
507
+ MEM_STATIC int ZSTD_cwksp_check_wasteful(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
508
+ return ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)
509
+ && ws->workspaceOversizedDuration > ZSTD_WORKSPACETOOLARGE_MAXDURATION;
510
+ }
511
+
512
+ MEM_STATIC void ZSTD_cwksp_bump_oversized_duration(
513
+ ZSTD_cwksp* ws, size_t additionalNeededSpace) {
514
+ if (ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)) {
515
+ ws->workspaceOversizedDuration++;
516
+ } else {
517
+ ws->workspaceOversizedDuration = 0;
518
+ }
519
+ }
520
+
521
+ #if defined (__cplusplus)
522
+ }
523
+ #endif
524
+
525
+ #endif /* ZSTD_CWKSP_H */