зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1654991 - Provide and use special case of ShrinkCapacityToZero. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D84805
This commit is contained in:
Родитель
fcbd7f145f
Коммит
45b8036770
|
@ -267,6 +267,27 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacity(
|
|||
mHdr->mCapacity = length;
|
||||
}
|
||||
|
||||
template <class Alloc, class RelocationStrategy>
|
||||
void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacityToZero(
|
||||
size_type aElemSize, size_t aElemAlign) {
|
||||
MOZ_ASSERT(mHdr->mLength == 0);
|
||||
|
||||
if (mHdr == EmptyHdr() || UsesAutoArrayBuffer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool isAutoArray = IsAutoArray();
|
||||
|
||||
nsTArrayFallibleAllocator::Free(mHdr);
|
||||
|
||||
if (isAutoArray) {
|
||||
mHdr = GetAutoArrayBufferUnsafe(aElemAlign);
|
||||
mHdr->mLength = 0;
|
||||
} else {
|
||||
mHdr = EmptyHdr();
|
||||
}
|
||||
}
|
||||
|
||||
template <class Alloc, class RelocationStrategy>
|
||||
template <typename ActualAlloc>
|
||||
void nsTArray_base<Alloc, RelocationStrategy>::ShiftData(index_type aStart,
|
||||
|
@ -284,7 +305,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShiftData(index_type aStart,
|
|||
// Compute the resulting length of the array
|
||||
mHdr->mLength += aNewLen - aOldLen;
|
||||
if (mHdr->mLength == 0) {
|
||||
ShrinkCapacity(aElemSize, aElemAlign);
|
||||
ShrinkCapacityToZero(aElemSize, aElemAlign);
|
||||
} else {
|
||||
// Maybe nothing needs to be shifted
|
||||
if (num == 0) {
|
||||
|
@ -320,7 +341,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::SwapFromEnd(index_type aStart,
|
|||
|
||||
if (mHdr->mLength == 0) {
|
||||
// If we have no elements remaining in the array, we can free our buffer.
|
||||
ShrinkCapacity(aElemSize, aElemAlign);
|
||||
ShrinkCapacityToZero(aElemSize, aElemAlign);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -457,6 +457,12 @@ class nsTArray_base {
|
|||
// @param aElemAlign The alignment in bytes of an array element.
|
||||
void ShrinkCapacity(size_type aElemSize, size_t aElemAlign);
|
||||
|
||||
// Resizes the storage to 0. This may only be called when Length() is already
|
||||
// 0.
|
||||
// @param aElemSize The size of an array element.
|
||||
// @param aElemAlign The alignment in bytes of an array element.
|
||||
void ShrinkCapacityToZero(size_type aElemSize, size_t aElemAlign);
|
||||
|
||||
// This method may be called to resize a "gap" in the array by shifting
|
||||
// elements around. It updates mLength appropriately. If the resulting
|
||||
// array has zero elements, then the array's memory is free'd.
|
||||
|
@ -1891,7 +1897,7 @@ class nsTArray_Impl
|
|||
|
||||
void Clear() {
|
||||
ClearAndRetainStorage();
|
||||
Compact();
|
||||
base_type::ShrinkCapacityToZero(sizeof(elem_type), MOZ_ALIGNOF(elem_type));
|
||||
}
|
||||
|
||||
// This method removes elements based on the return value of the
|
||||
|
|
Загрузка…
Ссылка в новой задаче