Skip to content

Commit 03533df

Browse files
pfactumptr1337
authored andcommitted
zstd: import upstream v1.5.6
Signed-off-by: Oleksandr Natalenko <[email protected]>
1 parent c133204 commit 03533df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+6576
-3530
lines changed

include/linux/zstd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
22
/*
3-
* Copyright (c) Yann Collet, Facebook, Inc.
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
44
* All rights reserved.
55
*
66
* This source code is licensed under both the BSD-style license (found in the

include/linux/zstd_errors.h

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
12
/*
2-
* Copyright (c) Yann Collet, Facebook, Inc.
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
34
* All rights reserved.
45
*
56
* This source code is licensed under both the BSD-style license (found in the
@@ -17,8 +18,17 @@
1718

1819

1920
/* ===== ZSTDERRORLIB_API : control library symbols visibility ===== */
20-
#define ZSTDERRORLIB_VISIBILITY
21-
#define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBILITY
21+
#define ZSTDERRORLIB_VISIBLE
22+
23+
#ifndef ZSTDERRORLIB_HIDDEN
24+
# if (__GNUC__ >= 4) && !defined(__MINGW32__)
25+
# define ZSTDERRORLIB_HIDDEN __attribute__ ((visibility ("hidden")))
26+
# else
27+
# define ZSTDERRORLIB_HIDDEN
28+
# endif
29+
#endif
30+
31+
#define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBLE
2232

2333
/*-*********************************************
2434
* Error codes list
@@ -43,26 +53,33 @@ typedef enum {
4353
ZSTD_error_frameParameter_windowTooLarge = 16,
4454
ZSTD_error_corruption_detected = 20,
4555
ZSTD_error_checksum_wrong = 22,
56+
ZSTD_error_literals_headerWrong = 24,
4657
ZSTD_error_dictionary_corrupted = 30,
4758
ZSTD_error_dictionary_wrong = 32,
4859
ZSTD_error_dictionaryCreation_failed = 34,
4960
ZSTD_error_parameter_unsupported = 40,
61+
ZSTD_error_parameter_combination_unsupported = 41,
5062
ZSTD_error_parameter_outOfBound = 42,
5163
ZSTD_error_tableLog_tooLarge = 44,
5264
ZSTD_error_maxSymbolValue_tooLarge = 46,
5365
ZSTD_error_maxSymbolValue_tooSmall = 48,
66+
ZSTD_error_stabilityCondition_notRespected = 50,
5467
ZSTD_error_stage_wrong = 60,
5568
ZSTD_error_init_missing = 62,
5669
ZSTD_error_memory_allocation = 64,
5770
ZSTD_error_workSpace_tooSmall= 66,
5871
ZSTD_error_dstSize_tooSmall = 70,
5972
ZSTD_error_srcSize_wrong = 72,
6073
ZSTD_error_dstBuffer_null = 74,
74+
ZSTD_error_noForwardProgress_destFull = 80,
75+
ZSTD_error_noForwardProgress_inputEmpty = 82,
6176
/* following error codes are __NOT STABLE__, they can be removed or changed in future versions */
6277
ZSTD_error_frameIndex_tooLarge = 100,
6378
ZSTD_error_seekableIO = 102,
6479
ZSTD_error_dstBuffer_wrong = 104,
6580
ZSTD_error_srcBuffer_wrong = 105,
81+
ZSTD_error_sequenceProducer_failed = 106,
82+
ZSTD_error_externalSequences_invalid = 107,
6683
ZSTD_error_maxCode = 120 /* never EVER use this value directly, it can change in future versions! Use ZSTD_isError() instead */
6784
} ZSTD_ErrorCode;
6885

include/linux/zstd_lib.h

+678-172
Large diffs are not rendered by default.

lib/zstd/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
22
# ################################################################
3-
# Copyright (c) Facebook, Inc.
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
44
# All rights reserved.
55
#
66
# This source code is licensed under both the BSD-style license (found in the

lib/zstd/common/allocations.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
2+
/*
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under both the BSD-style license (found in the
7+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
8+
* in the COPYING file in the root directory of this source tree).
9+
* You may select, at your option, one of the above-listed licenses.
10+
*/
11+
12+
/* This file provides custom allocation primitives
13+
*/
14+
15+
#define ZSTD_DEPS_NEED_MALLOC
16+
#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
17+
18+
#include "compiler.h" /* MEM_STATIC */
19+
#define ZSTD_STATIC_LINKING_ONLY
20+
#include <linux/zstd.h> /* ZSTD_customMem */
21+
22+
#ifndef ZSTD_ALLOCATIONS_H
23+
#define ZSTD_ALLOCATIONS_H
24+
25+
/* custom memory allocation functions */
26+
27+
MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
28+
{
29+
if (customMem.customAlloc)
30+
return customMem.customAlloc(customMem.opaque, size);
31+
return ZSTD_malloc(size);
32+
}
33+
34+
MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
35+
{
36+
if (customMem.customAlloc) {
37+
/* calloc implemented as malloc+memset;
38+
* not as efficient as calloc, but next best guess for custom malloc */
39+
void* const ptr = customMem.customAlloc(customMem.opaque, size);
40+
ZSTD_memset(ptr, 0, size);
41+
return ptr;
42+
}
43+
return ZSTD_calloc(1, size);
44+
}
45+
46+
MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
47+
{
48+
if (ptr!=NULL) {
49+
if (customMem.customFree)
50+
customMem.customFree(customMem.opaque, ptr);
51+
else
52+
ZSTD_free(ptr);
53+
}
54+
}
55+
56+
#endif /* ZSTD_ALLOCATIONS_H */

lib/zstd/common/bits.h

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
2+
/*
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under both the BSD-style license (found in the
7+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
8+
* in the COPYING file in the root directory of this source tree).
9+
* You may select, at your option, one of the above-listed licenses.
10+
*/
11+
12+
#ifndef ZSTD_BITS_H
13+
#define ZSTD_BITS_H
14+
15+
#include "mem.h"
16+
17+
MEM_STATIC unsigned ZSTD_countTrailingZeros32_fallback(U32 val)
18+
{
19+
assert(val != 0);
20+
{
21+
static const U32 DeBruijnBytePos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
22+
30, 22, 20, 15, 25, 17, 4, 8,
23+
31, 27, 13, 23, 21, 19, 16, 7,
24+
26, 12, 18, 6, 11, 5, 10, 9};
25+
return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >> 27];
26+
}
27+
}
28+
29+
MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val)
30+
{
31+
assert(val != 0);
32+
# if (__GNUC__ >= 4)
33+
return (unsigned)__builtin_ctz(val);
34+
# else
35+
return ZSTD_countTrailingZeros32_fallback(val);
36+
# endif
37+
}
38+
39+
MEM_STATIC unsigned ZSTD_countLeadingZeros32_fallback(U32 val) {
40+
assert(val != 0);
41+
{
42+
static const U32 DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29,
43+
11, 14, 16, 18, 22, 25, 3, 30,
44+
8, 12, 20, 28, 15, 17, 24, 7,
45+
19, 27, 23, 6, 26, 5, 4, 31};
46+
val |= val >> 1;
47+
val |= val >> 2;
48+
val |= val >> 4;
49+
val |= val >> 8;
50+
val |= val >> 16;
51+
return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27];
52+
}
53+
}
54+
55+
MEM_STATIC unsigned ZSTD_countLeadingZeros32(U32 val)
56+
{
57+
assert(val != 0);
58+
# if (__GNUC__ >= 4)
59+
return (unsigned)__builtin_clz(val);
60+
# else
61+
return ZSTD_countLeadingZeros32_fallback(val);
62+
# endif
63+
}
64+
65+
MEM_STATIC unsigned ZSTD_countTrailingZeros64(U64 val)
66+
{
67+
assert(val != 0);
68+
# if (__GNUC__ >= 4) && defined(__LP64__)
69+
return (unsigned)__builtin_ctzll(val);
70+
# else
71+
{
72+
U32 mostSignificantWord = (U32)(val >> 32);
73+
U32 leastSignificantWord = (U32)val;
74+
if (leastSignificantWord == 0) {
75+
return 32 + ZSTD_countTrailingZeros32(mostSignificantWord);
76+
} else {
77+
return ZSTD_countTrailingZeros32(leastSignificantWord);
78+
}
79+
}
80+
# endif
81+
}
82+
83+
MEM_STATIC unsigned ZSTD_countLeadingZeros64(U64 val)
84+
{
85+
assert(val != 0);
86+
# if (__GNUC__ >= 4)
87+
return (unsigned)(__builtin_clzll(val));
88+
# else
89+
{
90+
U32 mostSignificantWord = (U32)(val >> 32);
91+
U32 leastSignificantWord = (U32)val;
92+
if (mostSignificantWord == 0) {
93+
return 32 + ZSTD_countLeadingZeros32(leastSignificantWord);
94+
} else {
95+
return ZSTD_countLeadingZeros32(mostSignificantWord);
96+
}
97+
}
98+
# endif
99+
}
100+
101+
MEM_STATIC unsigned ZSTD_NbCommonBytes(size_t val)
102+
{
103+
if (MEM_isLittleEndian()) {
104+
if (MEM_64bits()) {
105+
return ZSTD_countTrailingZeros64((U64)val) >> 3;
106+
} else {
107+
return ZSTD_countTrailingZeros32((U32)val) >> 3;
108+
}
109+
} else { /* Big Endian CPU */
110+
if (MEM_64bits()) {
111+
return ZSTD_countLeadingZeros64((U64)val) >> 3;
112+
} else {
113+
return ZSTD_countLeadingZeros32((U32)val) >> 3;
114+
}
115+
}
116+
}
117+
118+
MEM_STATIC unsigned ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
119+
{
120+
assert(val != 0);
121+
return 31 - ZSTD_countLeadingZeros32(val);
122+
}
123+
124+
/* ZSTD_rotateRight_*():
125+
* Rotates a bitfield to the right by "count" bits.
126+
* https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts
127+
*/
128+
MEM_STATIC
129+
U64 ZSTD_rotateRight_U64(U64 const value, U32 count) {
130+
assert(count < 64);
131+
count &= 0x3F; /* for fickle pattern recognition */
132+
return (value >> count) | (U64)(value << ((0U - count) & 0x3F));
133+
}
134+
135+
MEM_STATIC
136+
U32 ZSTD_rotateRight_U32(U32 const value, U32 count) {
137+
assert(count < 32);
138+
count &= 0x1F; /* for fickle pattern recognition */
139+
return (value >> count) | (U32)(value << ((0U - count) & 0x1F));
140+
}
141+
142+
MEM_STATIC
143+
U16 ZSTD_rotateRight_U16(U16 const value, U32 count) {
144+
assert(count < 16);
145+
count &= 0x0F; /* for fickle pattern recognition */
146+
return (value >> count) | (U16)(value << ((0U - count) & 0x0F));
147+
}
148+
149+
#endif /* ZSTD_BITS_H */

0 commit comments

Comments
 (0)