[sanitizer] Fix InternalMmapVectorNoCtor reserve and resize

Remap on reserve of more than the current size.
Don't remap on downsize.

llvm-svn: 331784
This commit is contained in:
Vitaly Buka 2018-05-08 17:59:44 +00:00
parent 1aea95a9ea
commit 7381f26b45
2 changed files with 29 additions and 5 deletions

View File

@ -458,7 +458,7 @@ class InternalMmapVectorNoCtor {
CHECK_LE(size_, capacity_);
if (size_ == capacity_) {
uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
Resize(new_capacity);
Realloc(new_capacity);
}
internal_memcpy(&data_[size_++], &element, sizeof(T));
}
@ -483,12 +483,13 @@ class InternalMmapVectorNoCtor {
return capacity_;
}
void reserve(uptr new_size) {
if (new_size >= size()) return;
Resize(new_size);
// Never downsize internal buffer.
if (new_size > capacity())
Realloc(new_size);
}
void resize(uptr new_size) {
Resize(new_size);
if (new_size > size_) {
reserve(new_size);
internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
}
size_ = new_size;
@ -517,7 +518,7 @@ class InternalMmapVectorNoCtor {
}
private:
void Resize(uptr new_capacity) {
void Realloc(uptr new_capacity) {
CHECK_GT(new_capacity, 0);
CHECK_LE(size_, new_capacity);
T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),

View File

@ -88,6 +88,29 @@ TEST(SanitizerCommon, MmapAlignedOrDieOnFatalError) {
}
}
TEST(SanitizerCommon, InternalMmapVectorReize) {
InternalMmapVector<uptr> v;
CHECK_EQ(0U, v.size());
CHECK_GE(v.capacity(), v.size());
v.reserve(1000);
CHECK_EQ(0U, v.size());
CHECK_GE(v.capacity(), 1000U);
v.resize(10000);
CHECK_EQ(10000U, v.size());
CHECK_GE(v.capacity(), v.size());
uptr cap = v.capacity();
v.resize(100);
CHECK_EQ(100U, v.size());
CHECK_EQ(v.capacity(), cap);
v.reserve(10);
CHECK_EQ(100U, v.size());
CHECK_EQ(v.capacity(), cap);
}
TEST(SanitizerCommon, InternalMmapVector) {
InternalMmapVector<uptr> vector;
for (uptr i = 0; i < 100; i++) {