зеркало из https://github.com/mozilla/pjs.git
Add nsAutoTArray and nsAutoTPtrArray. b=356299 r/sr=darin
This commit is contained in:
Родитель
bb77a90665
Коммит
58bd3889ba
|
@ -42,7 +42,7 @@
|
|||
#include "nsXPCOM.h"
|
||||
#include "nsDebug.h"
|
||||
|
||||
const nsTArray_base::Header nsTArray_base::sEmptyHdr = { 0, 0 };
|
||||
nsTArray_base::Header nsTArray_base::sEmptyHdr = { 0, 0, 0 };
|
||||
|
||||
#ifdef NS_BUILD_REFCNT_LOGGING
|
||||
nsTArray_base::nsTArray_base()
|
||||
|
@ -57,13 +57,19 @@ nsTArray_base::~nsTArray_base() {
|
|||
|
||||
PRBool
|
||||
nsTArray_base::EnsureCapacity(size_type capacity, size_type elemSize) {
|
||||
// This should be the most common case so test this first
|
||||
if (capacity <= mHdr->mCapacity)
|
||||
return PR_TRUE;
|
||||
|
||||
// If the requested memory allocation exceeds size_type(-1)/2, then our
|
||||
// doubling algorithm may not be able to allocate it. Just bail out in
|
||||
// cases like that. We don't want to be allocating 2 GB+ arrays anyway.
|
||||
// doubling algorithm may not be able to allocate it. Additionally we
|
||||
// couldn't fit in the Header::mCapacity member. Just bail out in cases
|
||||
// like that. We don't want to be allocating 2 GB+ arrays anyway.
|
||||
if (capacity * elemSize > size_type(-1)/2) {
|
||||
NS_ERROR("Attempting to allocate excessively large array");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
if (mHdr == &sEmptyHdr) {
|
||||
// NS_Alloc new data
|
||||
Header *header = NS_STATIC_CAST(Header*,
|
||||
|
@ -72,50 +78,77 @@ nsTArray_base::EnsureCapacity(size_type capacity, size_type elemSize) {
|
|||
return PR_FALSE;
|
||||
header->mLength = 0;
|
||||
header->mCapacity = capacity;
|
||||
header->mIsAutoArray = 0;
|
||||
mHdr = header;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
// Use doubling algorithm when forced to increase available capacity.
|
||||
NS_ASSERTION(mHdr->mCapacity > 0, "should not have buffer of zero size");
|
||||
size_type temp = mHdr->mCapacity;
|
||||
while (temp < capacity)
|
||||
temp <<= 1;
|
||||
capacity = temp;
|
||||
|
||||
Header *header;
|
||||
if (UsesAutoArrayBuffer()) {
|
||||
// NS_Alloc and copy
|
||||
header = NS_STATIC_CAST(Header*,
|
||||
NS_Alloc(sizeof(Header) + capacity * elemSize));
|
||||
if (!header)
|
||||
return PR_FALSE;
|
||||
|
||||
memcpy(header, mHdr, sizeof(Header) + Length() * elemSize);
|
||||
} else {
|
||||
// NS_Realloc existing data
|
||||
if (capacity <= mHdr->mCapacity)
|
||||
return PR_TRUE;
|
||||
|
||||
// Use doubling algorithm when forced to increase available capacity.
|
||||
if (mHdr->mCapacity > 0) {
|
||||
size_type temp = mHdr->mCapacity;
|
||||
while (temp < capacity)
|
||||
temp <<= 1;
|
||||
capacity = temp;
|
||||
}
|
||||
|
||||
size_type size = sizeof(Header) + capacity * elemSize;
|
||||
void *ptr = NS_Realloc(mHdr, size);
|
||||
if (!ptr)
|
||||
header = NS_STATIC_CAST(Header*, NS_Realloc(mHdr, size));
|
||||
if (!header)
|
||||
return PR_FALSE;
|
||||
mHdr = NS_STATIC_CAST(Header*, ptr);
|
||||
mHdr->mCapacity = capacity;
|
||||
}
|
||||
|
||||
header->mCapacity = capacity;
|
||||
mHdr = header;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
nsTArray_base::ShrinkCapacity(size_type elemSize) {
|
||||
if (mHdr == &sEmptyHdr)
|
||||
if (mHdr == &sEmptyHdr || UsesAutoArrayBuffer())
|
||||
return;
|
||||
|
||||
if (mHdr->mLength >= mHdr->mCapacity) // should never be greater than...
|
||||
return;
|
||||
|
||||
if (mHdr->mLength == 0) {
|
||||
size_type length = Length();
|
||||
|
||||
if (IsAutoArray() && GetAutoArrayBuffer()->mCapacity >= length) {
|
||||
Header* header = GetAutoArrayBuffer();
|
||||
|
||||
// copy data, but don't copy the header to avoid overwritng mCapacity
|
||||
header->mLength = length;
|
||||
memcpy(header + 1, mHdr + 1, length * elemSize);
|
||||
|
||||
NS_Free(mHdr);
|
||||
mHdr = NS_CONST_CAST(Header *, &sEmptyHdr);
|
||||
mHdr = header;
|
||||
return;
|
||||
}
|
||||
|
||||
size_type size = sizeof(Header) + mHdr->mLength * elemSize;
|
||||
if (length == 0) {
|
||||
NS_ASSERTION(!IsAutoArray(), "autoarray should have fit 0 elements");
|
||||
NS_Free(mHdr);
|
||||
mHdr = &sEmptyHdr;
|
||||
return;
|
||||
}
|
||||
|
||||
size_type size = sizeof(Header) + length * elemSize;
|
||||
void *ptr = NS_Realloc(mHdr, size);
|
||||
if (!ptr)
|
||||
return;
|
||||
mHdr = NS_STATIC_CAST(Header*, ptr);
|
||||
mHdr->mCapacity = mHdr->mLength;
|
||||
mHdr->mCapacity = length;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -163,3 +196,86 @@ nsTArray_base::InsertSlotsAt(index_type index, size_type count,
|
|||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsTArray_base::SwapArrayElements(nsTArray_base& other, size_type elemSize)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
PRBool isAuto = IsAutoArray();
|
||||
PRBool otherIsAuto = other.IsAutoArray();
|
||||
#endif
|
||||
|
||||
if (!EnsureNotUsingAutoArrayBuffer(elemSize) ||
|
||||
!other.EnsureNotUsingAutoArrayBuffer(elemSize)) {
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
NS_ASSERTION(isAuto == IsAutoArray(), "lost auto info");
|
||||
NS_ASSERTION(otherIsAuto == other.IsAutoArray(), "lost auto info");
|
||||
NS_ASSERTION(!UsesAutoArrayBuffer() && !other.UsesAutoArrayBuffer(),
|
||||
"both should be using an alloced buffer now");
|
||||
|
||||
// If the two arrays have different mIsAutoArray values (i.e. one is an
|
||||
// autoarray and one is not) then simply switching the buffers is going to
|
||||
// make that bit wrong. We therefore adjust these mIsAutoArray bits before
|
||||
// switching the buffers so that once the buffers are switched the
|
||||
// mIsAutoArray bits are right again.
|
||||
// However, we have to watch out so that we don't set the bit on
|
||||
// sEmptyHeader. If an array (A) uses the empty header (and the other (B)
|
||||
// therefore must be an nsAutoTArray) we make A point to the B's autobuffer
|
||||
// so that when the buffers are switched B points to its own autobuffer.
|
||||
|
||||
// Adjust mIsAutoArray flags before swapping the buffers
|
||||
if (IsAutoArray() && !other.IsAutoArray()) {
|
||||
if (other.mHdr == &sEmptyHdr) {
|
||||
// Set other to use our built-in buffer so that we use it
|
||||
// after the swap below.
|
||||
other.mHdr = GetAutoArrayBuffer();
|
||||
other.mHdr->mLength = 0;
|
||||
}
|
||||
else {
|
||||
other.mHdr->mIsAutoArray = 1;
|
||||
}
|
||||
mHdr->mIsAutoArray = 0;
|
||||
}
|
||||
else if (!IsAutoArray() && other.IsAutoArray()) {
|
||||
if (mHdr == &sEmptyHdr) {
|
||||
// Set us to use other's built-in buffer so that other use it
|
||||
// after the swap below.
|
||||
mHdr = other.GetAutoArrayBuffer();
|
||||
mHdr->mLength = 0;
|
||||
}
|
||||
else {
|
||||
mHdr->mIsAutoArray = 1;
|
||||
}
|
||||
other.mHdr->mIsAutoArray = 0;
|
||||
}
|
||||
|
||||
// Swap the buffers
|
||||
Header *h = other.mHdr;
|
||||
other.mHdr = mHdr;
|
||||
mHdr = h;
|
||||
|
||||
NS_ASSERTION(isAuto == IsAutoArray(), "lost auto info");
|
||||
NS_ASSERTION(otherIsAuto == other.IsAutoArray(), "lost auto info");
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsTArray_base::EnsureNotUsingAutoArrayBuffer(size_type elemSize)
|
||||
{
|
||||
if (UsesAutoArrayBuffer()) {
|
||||
size_type size = sizeof(Header) + Length() * elemSize;
|
||||
|
||||
Header* header = NS_STATIC_CAST(Header*, NS_Alloc(size));
|
||||
if (!header)
|
||||
return PR_FALSE;
|
||||
|
||||
memcpy(header, mHdr, size);
|
||||
header->mCapacity = Length();
|
||||
mHdr = header;
|
||||
}
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче