Debian Codegraph @

3rdparty/tracy/public/common/tracy_lz4hc.hpp

raw symbols SCIP
1 /*
2 LZ4 HC - High Compression Mode of LZ4
3 Header File
4 Copyright (C) 2011-2020, Yann Collet.
5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 You can contact the author at :
31 - LZ4 source repository : https://github.com/lz4/lz4
32 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
33 */
34 #ifndef TRACY_LZ4_HC_H_19834876238432
35 #define TRACY_LZ4_HC_H_19834876238432
36
37 /* --- Dependency --- */
38 /* note : lz4hc requires lz4.h/lz4.c for compilation */
39 #include "tracy_lz4.hpp" /* stddef, LZ4LIB_API, LZ4_DEPRECATED */
40
41
42 /* --- Useful constants --- */
43 #define LZ4HC_CLEVEL_MIN 3
44 #define LZ4HC_CLEVEL_DEFAULT 9
45 #define LZ4HC_CLEVEL_OPT_MIN 10
46 #define LZ4HC_CLEVEL_MAX 12
47
48 namespace tracy
49 {
50
51 /*-************************************
52 * Block Compression
53 **************************************/
54 /*! LZ4_compress_HC() :
55 * Compress data from `src` into `dst`, using the powerful but slower "HC" algorithm.
56 * `dst` must be already allocated.
57 * Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` (see "lz4.h")
58 * Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE (see "lz4.h")
59 * `compressionLevel` : any value between 1 and LZ4HC_CLEVEL_MAX will work.
60 * Values > LZ4HC_CLEVEL_MAX behave the same as LZ4HC_CLEVEL_MAX.
61 * @return : the number of bytes written into 'dst'
62 * or 0 if compression fails.
63 */
64 LZ4LIB_API int LZ4_compress_HC (const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel);
65
66
67 /* Note :
68 * Decompression functions are provided within "lz4.h" (BSD license)
69 */
70
71
72 /*! LZ4_compress_HC_extStateHC() :
73 * Same as LZ4_compress_HC(), but using an externally allocated memory segment for `state`.
74 * `state` size is provided by LZ4_sizeofStateHC().
75 * Memory segment must be aligned on 8-bytes boundaries (which a normal malloc() should do properly).
76 */
77 LZ4LIB_API int LZ4_sizeofStateHC(void);
78 LZ4LIB_API int LZ4_compress_HC_extStateHC(void* stateHC, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
79
80
81 /*! LZ4_compress_HC_destSize() : v1.9.0+
82 * Will compress as much data as possible from `src`
83 * to fit into `targetDstSize` budget.
84 * Result is provided in 2 parts :
85 * @return : the number of bytes written into 'dst' (necessarily <= targetDstSize)
86 * or 0 if compression fails.
87 * `srcSizePtr` : on success, *srcSizePtr is updated to indicate how much bytes were read from `src`
88 */
89 LZ4LIB_API int LZ4_compress_HC_destSize(void* stateHC,
90 const char* src, char* dst,
91 int* srcSizePtr, int targetDstSize,
92 int compressionLevel);
93
94
95 /*-************************************
96 * Streaming Compression
97 * Bufferless synchronous API
98 **************************************/
99 typedef union LZ4_streamHC_u LZ4_streamHC_t; /* incomplete type (defined later) */
100
101 /*! LZ4_createStreamHC() and LZ4_freeStreamHC() :
102 * These functions create and release memory for LZ4 HC streaming state.
103 * Newly created states are automatically initialized.
104 * A same state can be used multiple times consecutively,
105 * starting with LZ4_resetStreamHC_fast() to start a new stream of blocks.
106 */
107 LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void);
108 LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
109
110 /*
111 These functions compress data in successive blocks of any size,
112 using previous blocks as dictionary, to improve compression ratio.
113 One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
114 There is an exception for ring buffers, which can be smaller than 64 KB.
115 Ring-buffer scenario is automatically detected and handled within LZ4_compress_HC_continue().
116
117 Before starting compression, state must be allocated and properly initialized.
118 LZ4_createStreamHC() does both, though compression level is set to LZ4HC_CLEVEL_DEFAULT.
119
120 Selecting the compression level can be done with LZ4_resetStreamHC_fast() (starts a new stream)
121 or LZ4_setCompressionLevel() (anytime, between blocks in the same stream) (experimental).
122 LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once,
123 which is automatically the case when state is created using LZ4_createStreamHC().
124
125 After reset, a first "fictional block" can be designated as initial dictionary,
126 using LZ4_loadDictHC() (Optional).
127
128 Invoke LZ4_compress_HC_continue() to compress each successive block.
129 The number of blocks is unlimited.
130 Previous input blocks, including initial dictionary when present,
131 must remain accessible and unmodified during compression.
132
133 It's allowed to update compression level anytime between blocks,
134 using LZ4_setCompressionLevel() (experimental).
135
136 'dst' buffer should be sized to handle worst case scenarios
137 (see LZ4_compressBound(), it ensures compression success).
138 In case of failure, the API does not guarantee recovery,
139 so the state _must_ be reset.
140 To ensure compression success
141 whenever `dst` buffer size cannot be made >= LZ4_compressBound(),
142 consider using LZ4_compress_HC_continue_destSize().
143
144 Whenever previous input blocks can't be preserved unmodified in-place during compression of next blocks,
145 it's possible to copy the last blocks into a more stable memory space, using LZ4_saveDictHC().
146 Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer' (<= 64 KB)
147
148 After completing a streaming compression,
149 it's possible to start a new stream of blocks, using the same LZ4_streamHC_t state,
150 just by resetting it, using LZ4_resetStreamHC_fast().
151 */
152
153 LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t* streamHCPtr, int compressionLevel); /* v1.9.0+ */
154 LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
155
156 LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr,
157 const char* src, char* dst,
158 int srcSize, int maxDstSize);
159
160 /*! LZ4_compress_HC_continue_destSize() : v1.9.0+
161 * Similar to LZ4_compress_HC_continue(),
162 * but will read as much data as possible from `src`
163 * to fit into `targetDstSize` budget.
164 * Result is provided into 2 parts :
165 * @return : the number of bytes written into 'dst' (necessarily <= targetDstSize)
166 * or 0 if compression fails.
167 * `srcSizePtr` : on success, *srcSizePtr will be updated to indicate how much bytes were read from `src`.
168 * Note that this function may not consume the entire input.
169 */
170 LZ4LIB_API int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
171 const char* src, char* dst,
172 int* srcSizePtr, int targetDstSize);
173
174 LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
175
176
177
178 /*^**********************************************
179 * !!!!!! STATIC LINKING ONLY !!!!!!
180 ***********************************************/
181
182 /*-******************************************************************
183 * PRIVATE DEFINITIONS :
184 * Do not use these definitions directly.
185 * They are merely exposed to allow static allocation of `LZ4_streamHC_t`.
186 * Declare an `LZ4_streamHC_t` directly, rather than any type below.
187 * Even then, only do so in the context of static linking, as definitions may change between versions.
188 ********************************************************************/
189
190 #define LZ4HC_DICTIONARY_LOGSIZE 16
191 #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
192 #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
193
194 #define LZ4HC_HASH_LOG 15
195 #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
196 #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
197
198
199 /* Never ever use these definitions directly !
200 * Declare or allocate an LZ4_streamHC_t instead.
201 **/
202 typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal;
203 struct LZ4HC_CCtx_internal
204 {
205 LZ4_u32 hashTable[LZ4HC_HASHTABLESIZE];
206 LZ4_u16 chainTable[LZ4HC_MAXD];
207 const LZ4_byte* end; /* next block here to continue on current prefix */
208 const LZ4_byte* prefixStart; /* Indexes relative to this position */
209 const LZ4_byte* dictStart; /* alternate reference for extDict */
210 LZ4_u32 dictLimit; /* below that point, need extDict */
211 LZ4_u32 lowLimit; /* below that point, no more dict */
212 LZ4_u32 nextToUpdate; /* index from which to continue dictionary update */
213 short compressionLevel;
214 LZ4_i8 favorDecSpeed; /* favor decompression speed if this flag set,
215 otherwise, favor compression ratio */
216 LZ4_i8 dirty; /* stream has to be fully reset if this flag is set */
217 const LZ4HC_CCtx_internal* dictCtx;
218 };
219
220 #define LZ4_STREAMHC_MINSIZE 262200 /* static size, for inter-version compatibility */
221 union LZ4_streamHC_u {
222 char minStateSize[LZ4_STREAMHC_MINSIZE];
223 LZ4HC_CCtx_internal internal_donotuse;
224 }; /* previously typedef'd to LZ4_streamHC_t */
225
226 /* LZ4_streamHC_t :
227 * This structure allows static allocation of LZ4 HC streaming state.
228 * This can be used to allocate statically on stack, or as part of a larger structure.
229 *
230 * Such state **must** be initialized using LZ4_initStreamHC() before first use.
231 *
232 * Note that invoking LZ4_initStreamHC() is not required when
233 * the state was created using LZ4_createStreamHC() (which is recommended).
234 * Using the normal builder, a newly created state is automatically initialized.
235 *
236 * Static allocation shall only be used in combination with static linking.
237 */
238
239 /* LZ4_initStreamHC() : v1.9.0+
240 * Required before first use of a statically allocated LZ4_streamHC_t.
241 * Before v1.9.0 : use LZ4_resetStreamHC() instead
242 */
243 LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC(void* buffer, size_t size);
244
245
246 /*-************************************
247 * Deprecated Functions
248 **************************************/
249 /* see lz4.h LZ4_DISABLE_DEPRECATE_WARNINGS to turn off deprecation warnings */
250
251 /* deprecated compression functions */
252 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC (const char* source, char* dest, int inputSize);
253 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
254 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel);
255 LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
256 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize);
257 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
258 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel);
259 LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
260 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize);
261 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
262
263 /* Obsolete streaming functions; degraded functionality; do not use!
264 *
265 * In order to perform streaming compression, these functions depended on data
266 * that is no longer tracked in the state. They have been preserved as well as
267 * possible: using them will still produce a correct output. However, use of
268 * LZ4_slideInputBufferHC() will truncate the history of the stream, rather
269 * than preserve a window-sized chunk of history.
270 */
271 #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
272 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API void* LZ4_createHC (const char* inputBuffer);
273 LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") LZ4LIB_API int LZ4_freeHC (void* LZ4HC_Data);
274 #endif
275 LZ4_DEPRECATED("use LZ4_saveDictHC() instead") LZ4LIB_API char* LZ4_slideInputBufferHC (void* LZ4HC_Data);
276 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel);
277 LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel);
278 LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API int LZ4_sizeofStreamStateHC(void);
279 LZ4_DEPRECATED("use LZ4_initStreamHC() instead") LZ4LIB_API int LZ4_resetStreamStateHC(void* state, char* inputBuffer);
280
281
282 /* LZ4_resetStreamHC() is now replaced by LZ4_initStreamHC().
283 * The intention is to emphasize the difference with LZ4_resetStreamHC_fast(),
284 * which is now the recommended function to start a new stream of blocks,
285 * but cannot be used to initialize a memory segment containing arbitrary garbage data.
286 *
287 * It is recommended to switch to LZ4_initStreamHC().
288 * LZ4_resetStreamHC() will generate deprecation warnings in a future version.
289 */
290 LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel);
291
292 }
293
294 #endif /* LZ4_HC_H_19834876238432 */
295
296
297 /*-**************************************************
298 * !!!!! STATIC LINKING ONLY !!!!!
299 * Following definitions are considered experimental.
300 * They should not be linked from DLL,
301 * as there is no guarantee of API stability yet.
302 * Prototypes will be promoted to "stable" status
303 * after successful usage in real-life scenarios.
304 ***************************************************/
305 #ifdef LZ4_HC_STATIC_LINKING_ONLY /* protection macro */
306 #ifndef TRACY_LZ4_HC_SLO_098092834
307 #define TRACY_LZ4_HC_SLO_098092834
308
309 #define LZ4_STATIC_LINKING_ONLY /* LZ4LIB_STATIC_API */
310 #include "tracy_lz4.hpp"
311
312 namespace tracy
313 {
314
315 /*! LZ4_setCompressionLevel() : v1.8.0+ (experimental)
316 * It's possible to change compression level
317 * between successive invocations of LZ4_compress_HC_continue*()
318 * for dynamic adaptation.
319 */
320 LZ4LIB_STATIC_API void LZ4_setCompressionLevel(
321 LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
322
323 /*! LZ4_favorDecompressionSpeed() : v1.8.2+ (experimental)
324 * Opt. Parser will favor decompression speed over compression ratio.
325 * Only applicable to levels >= LZ4HC_CLEVEL_OPT_MIN.
326 */
327 LZ4LIB_STATIC_API void LZ4_favorDecompressionSpeed(
328 LZ4_streamHC_t* LZ4_streamHCPtr, int favor);
329
330 /*! LZ4_resetStreamHC_fast() : v1.9.0+
331 * When an LZ4_streamHC_t is known to be in a internally coherent state,
332 * it can often be prepared for a new compression with almost no work, only
333 * sometimes falling back to the full, expensive reset that is always required
334 * when the stream is in an indeterminate state (i.e., the reset performed by
335 * LZ4_resetStreamHC()).
336 *
337 * LZ4_streamHCs are guaranteed to be in a valid state when:
338 * - returned from LZ4_createStreamHC()
339 * - reset by LZ4_resetStreamHC()
340 * - memset(stream, 0, sizeof(LZ4_streamHC_t))
341 * - the stream was in a valid state and was reset by LZ4_resetStreamHC_fast()
342 * - the stream was in a valid state and was then used in any compression call
343 * that returned success
344 * - the stream was in an indeterminate state and was used in a compression
345 * call that fully reset the state (LZ4_compress_HC_extStateHC()) and that
346 * returned success
347 *
348 * Note:
349 * A stream that was last used in a compression call that returned an error
350 * may be passed to this function. However, it will be fully reset, which will
351 * clear any existing history and settings from the context.
352 */
353 LZ4LIB_STATIC_API void LZ4_resetStreamHC_fast(
354 LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
355
356 /*! LZ4_compress_HC_extStateHC_fastReset() :
357 * A variant of LZ4_compress_HC_extStateHC().
358 *
359 * Using this variant avoids an expensive initialization step. It is only safe
360 * to call if the state buffer is known to be correctly initialized already
361 * (see above comment on LZ4_resetStreamHC_fast() for a definition of
362 * "correctly initialized"). From a high level, the difference is that this
363 * function initializes the provided state with a call to
364 * LZ4_resetStreamHC_fast() while LZ4_compress_HC_extStateHC() starts with a
365 * call to LZ4_resetStreamHC().
366 */
367 LZ4LIB_STATIC_API int LZ4_compress_HC_extStateHC_fastReset (
368 void* state,
369 const char* src, char* dst,
370 int srcSize, int dstCapacity,
371 int compressionLevel);
372
373 /*! LZ4_attach_HC_dictionary() :
374 * This is an experimental API that allows for the efficient use of a
375 * static dictionary many times.
376 *
377 * Rather than re-loading the dictionary buffer into a working context before
378 * each compression, or copying a pre-loaded dictionary's LZ4_streamHC_t into a
379 * working LZ4_streamHC_t, this function introduces a no-copy setup mechanism,
380 * in which the working stream references the dictionary stream in-place.
381 *
382 * Several assumptions are made about the state of the dictionary stream.
383 * Currently, only streams which have been prepared by LZ4_loadDictHC() should
384 * be expected to work.
385 *
386 * Alternatively, the provided dictionary stream pointer may be NULL, in which
387 * case any existing dictionary stream is unset.
388 *
389 * A dictionary should only be attached to a stream without any history (i.e.,
390 * a stream that has just been reset).
391 *
392 * The dictionary will remain attached to the working stream only for the
393 * current stream session. Calls to LZ4_resetStreamHC(_fast) will remove the
394 * dictionary context association from the working stream. The dictionary
395 * stream (and source buffer) must remain in-place / accessible / unchanged
396 * through the lifetime of the stream session.
397 */
398 LZ4LIB_STATIC_API void LZ4_attach_HC_dictionary(
399 LZ4_streamHC_t *working_stream,
400 const LZ4_streamHC_t *dictionary_stream);
401
402 }
403
404 #endif /* LZ4_HC_SLO_098092834 */
405 #endif /* LZ4_HC_STATIC_LINKING_ONLY */