LLVM  15.0.0git
Compression.cpp
Go to the documentation of this file.
1 //===--- Compression.cpp - Compression implementation ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements compression functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Error.h"
20 #if LLVM_ENABLE_ZLIB
21 #include <zlib.h>
22 #endif
23 
24 using namespace llvm;
25 
26 #if LLVM_ENABLE_ZLIB
28  return make_error<StringError>(Err, inconvertibleErrorCode());
29 }
30 
32  switch (Code) {
33  case Z_MEM_ERROR:
34  return "zlib error: Z_MEM_ERROR";
35  case Z_BUF_ERROR:
36  return "zlib error: Z_BUF_ERROR";
37  case Z_STREAM_ERROR:
38  return "zlib error: Z_STREAM_ERROR";
39  case Z_DATA_ERROR:
40  return "zlib error: Z_DATA_ERROR";
41  case Z_OK:
42  default:
43  llvm_unreachable("unknown or unexpected zlib status code");
44  }
45 }
46 
47 bool zlib::isAvailable() { return true; }
48 
49 void zlib::compress(StringRef InputBuffer,
50  SmallVectorImpl<char> &CompressedBuffer, int Level) {
51  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52  CompressedBuffer.resize_for_overwrite(CompressedSize);
53  int Res =
54  ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
55  (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
56  if (Res == Z_MEM_ERROR)
57  report_bad_alloc_error("Allocation failed");
58  assert(Res == Z_OK);
59  // Tell MemorySanitizer that zlib output buffer is fully initialized.
60  // This avoids a false report when running LLVM with uninstrumented ZLib.
61  __msan_unpoison(CompressedBuffer.data(), CompressedSize);
62  CompressedBuffer.truncate(CompressedSize);
63 }
64 
65 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
66  size_t &UncompressedSize) {
67  int Res =
68  ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
69  (const Bytef *)InputBuffer.data(), InputBuffer.size());
70  // Tell MemorySanitizer that zlib output buffer is fully initialized.
71  // This avoids a false report when running LLVM with uninstrumented ZLib.
72  __msan_unpoison(UncompressedBuffer, UncompressedSize);
73  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
74 }
75 
77  SmallVectorImpl<char> &UncompressedBuffer,
78  size_t UncompressedSize) {
79  UncompressedBuffer.resize_for_overwrite(UncompressedSize);
80  Error E =
81  uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
82  UncompressedBuffer.truncate(UncompressedSize);
83  return E;
84 }
85 
87  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
88 }
89 
90 #else
91 bool zlib::isAvailable() { return false; }
92 void zlib::compress(StringRef InputBuffer,
93  SmallVectorImpl<char> &CompressedBuffer, int Level) {
94  llvm_unreachable("zlib::compress is unavailable");
95 }
96 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
97  size_t &UncompressedSize) {
98  llvm_unreachable("zlib::uncompress is unavailable");
99 }
100 Error zlib::uncompress(StringRef InputBuffer,
101  SmallVectorImpl<char> &UncompressedBuffer,
102  size_t UncompressedSize) {
103  llvm_unreachable("zlib::uncompress is unavailable");
104 }
106  llvm_unreachable("zlib::crc32 is unavailable");
107 }
108 #endif
Compression.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
StringRef.h
ErrorHandling.h
llvm::zlib::uncompress
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:65
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
Error.h
llvm::SmallVectorImpl::truncate
void truncate(size_type N)
Like resize, but requires that N is less than size().
Definition: SmallVector.h:625
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::zlib::compress
void compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
Definition: Compression.cpp:49
llvm::createError
static Error createError(const Twine &Err)
Definition: APFloat.cpp:232
convertZlibCodeToString
static StringRef convertZlibCodeToString(int Code)
Definition: Compression.cpp:31
llvm::SmallVectorImpl::resize_for_overwrite
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:622
llvm::report_bad_alloc_error
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
Definition: ErrorHandling.cpp:143
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::zlib::isAvailable
bool isAvailable()
Definition: Compression.cpp:47
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
__msan_unpoison
#define __msan_unpoison(p, size)
Definition: Compiler.h:408
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
uint32_t
Compiler.h
llvm::PICLevel::Level
Level
Definition: CodeGen.h:33
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
SmallVector.h
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::zlib::crc32
uint32_t crc32(StringRef Buffer)
Definition: Compression.cpp:86
llvm::SmallVectorImpl< char >