[PATCH] D50223: [Support] Add zlib::compress which operates on std::unique_ptr<uint8_t[]>

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 2 21:51:59 PDT 2018


MaskRay created this revision.
MaskRay added reviewers: ruiu, dblaikie.
Herald added a subscriber: llvm-commits.

SmallVectorImpl<char> is initialized (zeroed) and resident.
Using std::unique_ptr<uint8_t[]> as compression buffer has the advantage
that it is uninitialized, pages not touched by ::compress2 are not
resident.

This will be used to store compression buffers in LLD (--compress-debug-sections).


Repository:
  rL LLVM

https://reviews.llvm.org/D50223

Files:
  include/llvm/Support/Compression.h
  lib/Support/Compression.cpp


Index: lib/Support/Compression.cpp
===================================================================
--- lib/Support/Compression.cpp
+++ lib/Support/Compression.cpp
@@ -73,6 +73,20 @@
   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
 }
 
+Error zlib::compress(StringRef Input,
+                     std::unique_ptr<uint8_t[]> &CompressedData,
+                     size_t &CompressedSize, CompressionLevel Level) {
+  CompressedSize = (size_t)::compressBound(Input.size());
+  CompressedData.reset(new uint8_t[CompressedSize]);
+  int CLevel = encodeZlibCompressionLevel(Level);
+  int Res = ::compress2(CompressedData.get(), &CompressedSize,
+                        (const Bytef *)Input.data(), Input.size(), CLevel);
+  // Tell MemorySanitizer that zlib output buffer is fully initialized.
+  // This avoids a false report when running LLVM with uninstrumented ZLib.
+  __msan_unpoison(CompressedData.get(), CompressedSize);
+  return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
+}
+
 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
                        size_t &UncompressedSize) {
   int Res =
Index: include/llvm/Support/Compression.h
===================================================================
--- include/llvm/Support/Compression.h
+++ include/llvm/Support/Compression.h
@@ -15,6 +15,7 @@
 #define LLVM_SUPPORT_COMPRESSION_H
 
 #include "llvm/Support/DataTypes.h"
+#include <memory>
 
 namespace llvm {
 template <typename T> class SmallVectorImpl;
@@ -35,6 +36,10 @@
 Error compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
                CompressionLevel Level = DefaultCompression);
 
+Error compress(StringRef Input, std::unique_ptr<uint8_t[]> &CompressedData,
+               size_t &CompressedSize,
+               CompressionLevel Level = DefaultCompression);
+
 Error uncompress(StringRef InputBuffer, char *UncompressedBuffer,
                  size_t &UncompressedSize);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50223.158913.patch
Type: text/x-patch
Size: 2004 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180803/403c5bb0/attachment-0001.bin>


More information about the llvm-commits mailing list