Skip to content

Commit

Permalink
[sanitizer] Use all available rounded up capacity
Browse files Browse the repository at this point in the history
Reviewers: eugenis

Subscribers: kubamracek, llvm-commits

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

llvm-svn: 331940
  • Loading branch information
vitalybuka committed May 9, 2018
1 parent 6f2cf73 commit c779388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
31 changes: 14 additions & 17 deletions compiler-rt/lib/sanitizer_common/sanitizer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,12 @@ template<typename T>
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];
Expand All @@ -444,8 +443,8 @@ class InternalMmapVectorNoCtor {
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);
}
Expand All @@ -468,9 +467,7 @@ class InternalMmapVectorNoCtor {
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())
Expand Down Expand Up @@ -502,25 +499,25 @@ class InternalMmapVectorNoCtor {

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_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ TEST(SanitizerCommon, MmapAlignedOrDieOnFatalError) {
}
}

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());
Expand Down

0 comments on commit c779388

Please sign in to comment.