[PATCH] D46617: [sanitizer] Use all available rounded up capacity

Vitaly Buka via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 9 16:17:55 PDT 2018


vitalybuka updated this revision to Diff 146028.
vitalybuka marked an inline comment as done.
vitalybuka added a comment.

rename


Repository:
  rL LLVM

https://reviews.llvm.org/D46617

Files:
  compiler-rt/lib/sanitizer_common/sanitizer_common.h
  compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cc


Index: compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cc
===================================================================
--- compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cc
+++ compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cc
@@ -88,6 +88,12 @@
   }
 }
 
+TEST(SanitizerCommon, InternalMmapVectorRoundUpCapacity) {
+  InternalMmapVector<uptr> v;
+  v.reserve(1);
+  CHECK_EQ(v.capacity(), GetPageSizeCached() / sizeof(uptr));
+}
+
 TEST(SanitizerCommon, InternalMmapVectorReize) {
   InternalMmapVector<uptr> v;
   CHECK_EQ(0U, v.size());
Index: compiler-rt/lib/sanitizer_common/sanitizer_common.h
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -428,13 +428,12 @@
 class InternalMmapVectorNoCtor {
  public:
   void Initialize(uptr initial_capacity) {
-    capacity_ = Max(initial_capacity, (uptr)1);
+    capacity_bytes_ = 0;
     size_ = 0;
-    data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVectorNoCtor");
-  }
-  void Destroy() {
-    UnmapOrDie(data_, capacity_ * sizeof(T));
+    data_ = 0;
+    reserve(initial_capacity);
   }
+  void Destroy() { UnmapOrDie(data_, capacity_bytes_); }
   T &operator[](uptr i) {
     CHECK_LT(i, size_);
     return data_[i];
@@ -444,8 +443,8 @@
     return data_[i];
   }
   void push_back(const T &element) {
-    CHECK_LE(size_, capacity_);
-    if (size_ == capacity_) {
+    CHECK_LE(size_, capacity());
+    if (size_ == capacity()) {
       uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
       Realloc(new_capacity);
     }
@@ -468,9 +467,7 @@
   T *data() {
     return data_;
   }
-  uptr capacity() const {
-    return capacity_;
-  }
+  uptr capacity() const { return capacity_bytes_ / sizeof(T); }
   void reserve(uptr new_size) {
     // Never downsize internal buffer.
     if (new_size > capacity())
@@ -502,25 +499,25 @@
 
   void swap(InternalMmapVectorNoCtor &other) {
     Swap(data_, other.data_);
-    Swap(capacity_, other.capacity_);
+    Swap(capacity_bytes_, other.capacity_bytes_);
     Swap(size_, other.size_);
   }
 
  private:
   void Realloc(uptr new_capacity) {
     CHECK_GT(new_capacity, 0);
     CHECK_LE(size_, new_capacity);
-    T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
-                                 "InternalMmapVector");
+    uptr new_capacity_bytes =
+        RoundUpTo(new_capacity * sizeof(T), GetPageSizeCached());
+    T *new_data = (T *)MmapOrDie(new_capacity_bytes, "InternalMmapVector");
     internal_memcpy(new_data, data_, size_ * sizeof(T));
-    T *old_data = data_;
+    UnmapOrDie(data_, capacity_bytes_);
     data_ = new_data;
-    UnmapOrDie(old_data, capacity_ * sizeof(T));
-    capacity_ = new_capacity;
+    capacity_bytes_ = new_capacity_bytes;
   }
 
   T *data_;
-  uptr capacity_;
+  uptr capacity_bytes_;
   uptr size_;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46617.146028.patch
Type: text/x-patch
Size: 2956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180509/c5a1e88d/attachment.bin>


More information about the llvm-commits mailing list