forked from OSchip/llvm-project
[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:
parent
1aea95a9ea
commit
7381f26b45
|
|
@ -458,7 +458,7 @@ class InternalMmapVectorNoCtor {
|
||||||
CHECK_LE(size_, capacity_);
|
CHECK_LE(size_, capacity_);
|
||||||
if (size_ == capacity_) {
|
if (size_ == capacity_) {
|
||||||
uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
|
uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
|
||||||
Resize(new_capacity);
|
Realloc(new_capacity);
|
||||||
}
|
}
|
||||||
internal_memcpy(&data_[size_++], &element, sizeof(T));
|
internal_memcpy(&data_[size_++], &element, sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
@ -483,12 +483,13 @@ class InternalMmapVectorNoCtor {
|
||||||
return capacity_;
|
return capacity_;
|
||||||
}
|
}
|
||||||
void reserve(uptr new_size) {
|
void reserve(uptr new_size) {
|
||||||
if (new_size >= size()) return;
|
// Never downsize internal buffer.
|
||||||
Resize(new_size);
|
if (new_size > capacity())
|
||||||
|
Realloc(new_size);
|
||||||
}
|
}
|
||||||
void resize(uptr new_size) {
|
void resize(uptr new_size) {
|
||||||
Resize(new_size);
|
|
||||||
if (new_size > size_) {
|
if (new_size > size_) {
|
||||||
|
reserve(new_size);
|
||||||
internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
|
internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
|
||||||
}
|
}
|
||||||
size_ = new_size;
|
size_ = new_size;
|
||||||
|
|
@ -517,7 +518,7 @@ class InternalMmapVectorNoCtor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Resize(uptr new_capacity) {
|
void Realloc(uptr new_capacity) {
|
||||||
CHECK_GT(new_capacity, 0);
|
CHECK_GT(new_capacity, 0);
|
||||||
CHECK_LE(size_, new_capacity);
|
CHECK_LE(size_, new_capacity);
|
||||||
T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
|
T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
TEST(SanitizerCommon, InternalMmapVector) {
|
||||||
InternalMmapVector<uptr> vector;
|
InternalMmapVector<uptr> vector;
|
||||||
for (uptr i = 0; i < 100; i++) {
|
for (uptr i = 0; i < 100; i++) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue