# HG changeset patch # User Jon Coppeard # Date 1603288236 0 # Wed Oct 21 13:50:36 2020 +0000 # Node ID 7e223284a9225c66b590aaad671c7448d1ff0b57 # Parent dfcb025567da9e33bf724520e0146fef3d776d5f 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 diff -r dfcb025567da -r 7e223284a922 xpcom/ds/nsTArray-inl.h --- a/xpcom/ds/nsTArray-inl.h Thu Oct 22 07:36:15 2020 +0000 +++ b/xpcom/ds/nsTArray-inl.h Wed Oct 21 13:50:36 2020 +0000 @@ -259,10 +259,27 @@ } 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(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(ptr); mHdr->mCapacity = length; }