[compiler-rt] 5f1d185 - [NFC][sanitizer] Iterator adaptors for Leb128 encoding

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 3 12:52:00 PST 2021


Author: Vitaly Buka
Date: 2021-12-03T12:51:55-08:00
New Revision: 5f1d1854eb1450d352663ee732235893c5782237

URL: https://github.com/llvm/llvm-project/commit/5f1d1854eb1450d352663ee732235893c5782237
DIFF: https://github.com/llvm/llvm-project/commit/5f1d1854eb1450d352663ee732235893c5782237.diff

LOG: [NFC][sanitizer] Iterator adaptors for Leb128 encoding

It's similar to back_insert_iterator

Needed for D114924

Reviewed By: morehouse, kstoimenov

Differential Revision: https://reviews.llvm.org/D114980

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
index d371a4fd1fd0..416e3b8c29fd 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
@@ -130,26 +130,76 @@ uptr *StackStore::BlockInfo::GetOrCreate() {
   return Create();
 }
 
+class SLeb128Encoder {
+ public:
+  SLeb128Encoder(u8 *begin, u8 *end) : begin(begin), end(end) {}
+
+  bool operator==(const SLeb128Encoder &other) const {
+    return begin == other.begin;
+  }
+
+  bool operator!=(const SLeb128Encoder &other) const {
+    return begin != other.begin;
+  }
+
+  SLeb128Encoder &operator=(uptr v) {
+    sptr 
diff  = v - previous;
+    begin = EncodeSLEB128(
diff , begin, end);
+    previous = v;
+    return *this;
+  }
+  SLeb128Encoder &operator*() { return *this; }
+  SLeb128Encoder &operator++() { return *this; }
+
+  u8 *base() const { return begin; }
+
+ private:
+  u8 *begin;
+  u8 *end;
+  uptr previous = 0;
+};
+
+class SLeb128Decoder {
+ public:
+  SLeb128Decoder(const u8 *begin, const u8 *end) : begin(begin), end(end) {}
+
+  bool operator==(const SLeb128Decoder &other) const {
+    return begin == other.begin;
+  }
+
+  bool operator!=(const SLeb128Decoder &other) const {
+    return begin != other.begin;
+  }
+
+  uptr operator*() {
+    sptr 
diff ;
+    begin = DecodeSLEB128(begin, end, &
diff );
+    previous += 
diff ;
+    return previous;
+  }
+  SLeb128Decoder &operator++() { return *this; }
+
+  SLeb128Decoder operator++(int) { return *this; }
+
+ private:
+  const u8 *begin;
+  const u8 *end;
+  uptr previous = 0;
+};
+
 static u8 *CompressDelta(const uptr *from, const uptr *from_end, u8 *to,
                          u8 *to_end) {
-  uptr prev = 0;
-  for (; from < from_end; ++from) {
-    sptr 
diff  = *from - prev;
-    to = EncodeSLEB128(
diff , to, to_end);
-    prev += 
diff ;
-  }
-  return to;
+  SLeb128Encoder encoder(to, to_end);
+  for (; from != from_end; ++from, ++encoder) *encoder = *from;
+  return encoder.base();
 }
 
 static uptr *UncompressDelta(const u8 *from, const u8 *from_end, uptr *to,
                              uptr *to_end) {
-  uptr prev = 0;
-  for (; to < to_end; ++to) {
-    sptr 
diff ;
-    from = DecodeSLEB128<sptr>(from, from_end, &
diff );
-    prev += 
diff ;
-    *to = prev;
-  }
+  SLeb128Decoder decoder(from, from_end);
+  SLeb128Decoder end(from_end, from_end);
+  for (; decoder != end; ++to, ++decoder) *to = *decoder;
+  CHECK_EQ(to, to_end);
   return to;
 }
 


        


More information about the llvm-commits mailing list