Bug 1670358 - Don't use realloc for shrinking nsTArrays and similar when RelocationStrategy::allowRealloc is false r=sg

My original patch handled the grow case but not the shrink case. When the
current and new allocation sizes are in different size classes jemalloc's
realloc will move the allocation when shrinking, not just truncate the existing
one.

Differential Revision: https://phabricator.services.mozilla.com/D93654
This commit is contained in:
Jon Coppeard 2020-10-21 13:50:36 +00:00
Родитель 70d15af587
Коммит 7da8f2fe49
1 изменённых файлов: 20 добавлений и 3 удалений

Просмотреть файл

@ -259,10 +259,27 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacity(
}
size_type size = sizeof(Header) + length * aElemSize;
void* ptr = nsTArrayFallibleAllocator::Realloc(mHdr, size);
if (!ptr) {
return;
void* ptr;
if (!RelocationStrategy::allowRealloc) {
// Malloc() and copy.
ptr = static_cast<Header*>(nsTArrayFallibleAllocator::Malloc(size));
if (!ptr) {
return;
}
RelocationStrategy::RelocateNonOverlappingRegionWithHeader(
ptr, mHdr, Length(), aElemSize);
nsTArrayFallibleAllocator::Free(mHdr);
} else {
// Realloc() existing data.
ptr = nsTArrayFallibleAllocator::Realloc(mHdr, size);
if (!ptr) {
return;
}
}
mHdr = static_cast<Header*>(ptr);
mHdr->mCapacity = length;
}