LLVM: lib/Support/Compression.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
16#include "llvm/Config/config.h"
20#if LLVM_ENABLE_ZLIB
21#include <zlib.h>
22#endif
23#if LLVM_ENABLE_ZSTD
24#include <zstd.h>
25#endif
26
27using namespace llvm;
29
31 switch (F) {
34 return nullptr;
35 return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
36 "build time";
39 return nullptr;
40 return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
41 "build time";
42 }
44}
45
48 switch (P.format) {
51 break;
54 break;
55 }
56}
57
59 uint8_t *Output, size_t UncompressedSize) {
65 }
67}
68
71 size_t UncompressedSize) {
72 switch (F) {
77 }
79}
80
83 size_t UncompressedSize) {
85}
86
87#if LLVM_ENABLE_ZLIB
88
89static StringRef convertZlibCodeToString(int Code) {
90 switch (Code) {
91 case Z_MEM_ERROR:
92 return "zlib error: Z_MEM_ERROR";
93 case Z_BUF_ERROR:
94 return "zlib error: Z_BUF_ERROR";
95 case Z_STREAM_ERROR:
96 return "zlib error: Z_STREAM_ERROR";
97 case Z_DATA_ERROR:
98 return "zlib error: Z_DATA_ERROR";
99 case Z_OK:
100 default:
102 }
103}
104
106
109 unsigned long CompressedSize = ::compressBound(Input.size());
111 int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
112 (const Bytef *)Input.data(), Input.size(), Level);
113 if (Res == Z_MEM_ERROR)
116
117
119 if (CompressedSize < CompressedBuffer.size())
120 CompressedBuffer.truncate(CompressedSize);
121}
122
124 size_t &UncompressedSize) {
125 int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
126 (const Bytef *)Input.data(), Input.size());
127
128
133}
134
137 size_t UncompressedSize) {
140 if (UncompressedSize < Output.size())
141 Output.truncate(UncompressedSize);
142 return E;
143}
144
145#else
152 size_t &UncompressedSize) {
154}
157 size_t UncompressedSize) {
159}
160#endif
161
162#if LLVM_ENABLE_ZSTD
163
165
166#include <zstd.h>
167
170 bool EnableLdm) {
171 ZSTD_CCtx *Cctx = ZSTD_createCCtx();
172 if (!Cctx)
174
175 if (ZSTD_isError(ZSTD_CCtx_setParameter(
176 Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
177 ZSTD_freeCCtx(Cctx);
179 }
180
181 if (ZSTD_isError(
182 ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
183 ZSTD_freeCCtx(Cctx);
185 }
186
187 unsigned long CompressedBufferSize = ZSTD_compressBound(Input.size());
189
190 size_t const CompressedSize =
191 ZSTD_compress2(Cctx, CompressedBuffer.data(), CompressedBufferSize,
193
194 ZSTD_freeCCtx(Cctx);
195
196 if (ZSTD_isError(CompressedSize))
198
200 if (CompressedSize < CompressedBuffer.size())
201 CompressedBuffer.truncate(CompressedSize);
202}
203
205 size_t &UncompressedSize) {
206 const size_t Res = ::ZSTD_decompress(
207 Output, UncompressedSize, (const uint8_t *)Input.data(), Input.size());
208 UncompressedSize = Res;
209 if (ZSTD_isError(Res))
212
213
216}
217
220 size_t UncompressedSize) {
223 if (UncompressedSize < Output.size())
224 Output.truncate(UncompressedSize);
225 return E;
226}
227
228#else
232 bool EnableLdm) {
234}
236 size_t &UncompressedSize) {
238}
241 size_t UncompressedSize) {
243}
244#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define __msan_unpoison(p, size)
This file defines the SmallVector class.
The Input class is used to parse a yaml document into in-memory structs and vectors.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
void truncate(size_type N)
Like resize, but requires that N is less than size().
pointer data()
Return a pointer to the vector's buffer, even if empty().
StringRef - Represent a constant reference to a string, i.e.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
Definition Compression.cpp:147
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Definition Compression.cpp:151
LLVM_ABI bool isAvailable()
Definition Compression.cpp:146
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Definition Compression.cpp:235
LLVM_ABI bool isAvailable()
Definition Compression.cpp:229
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
Definition Compression.cpp:230
LLVM_ABI const char * getReasonIfUnsupported(Format F)
Definition Compression.cpp:30
LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Definition Compression.cpp:58
Format formatFor(DebugCompressionType Type)
LLVM_ABI void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
Definition Compression.cpp:46
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
LogicalResult success(bool IsSuccess=true)
Utility function to generate a LogicalResult.
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.