Make nsDeque handle allocation failure better. (Bug 520661) r=bsmedberg

This commit is contained in:
L. David Baron 2009-10-07 20:22:42 -07:00
Родитель d608e40aec
Коммит 0a6a402714
2 изменённых файлов: 23 добавлений и 23 удалений

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

@ -174,36 +174,32 @@ nsDeque& nsDeque::Erase() {
* and you knew its capacity beforehand,
* then this would be a way to indicate the failure.
*/
PRInt32 nsDeque::GrowCapacity() {
PRBool nsDeque::GrowCapacity() {
PRInt32 theNewSize=mCapacity<<2;
NS_ASSERTION(theNewSize>mCapacity, "Overflow");
if (theNewSize<=mCapacity)
return mCapacity;
return PR_FALSE;
void** temp=new void*[theNewSize];
if (!temp)
return PR_FALSE;
//Here's the interesting part: You can't just move the elements
//directly (in situ) from the old buffer to the new one.
//Since capacity has changed, the old origin doesn't make
//sense anymore. It's better to resequence the elements now.
if (temp) {
PRInt32 tempi=0;
PRInt32 i=0;
PRInt32 j=0;
for (i=mOrigin; i<mCapacity; i++) {
temp[tempi++]=mData[i]; //copy the leading elements...
}
for (j=0;j<mOrigin;j++) {
temp[tempi++]=mData[j]; //copy the trailing elements...
}
if (mData != mBuffer) {
delete [] mData;
}
mCapacity=theNewSize;
mOrigin=0; //now realign the origin...
mData=temp;
memcpy(temp, mData + mOrigin, sizeof(void*) * (mCapacity - mOrigin));
memcpy(temp + (mCapacity - mOrigin), mData, sizeof(void*) * mOrigin);
if (mData != mBuffer) {
delete [] mData;
}
return mCapacity;
mCapacity=theNewSize;
mOrigin=0; //now realign the origin...
mData=temp;
return PR_TRUE;
}
/**
@ -215,8 +211,9 @@ PRInt32 nsDeque::GrowCapacity() {
* @return *this
*/
nsDeque& nsDeque::Push(void* aItem) {
if (mSize==mCapacity) {
GrowCapacity();
if (mSize==mCapacity && !GrowCapacity()) {
NS_WARNING("out of memory");
return *this;
}
mData[modulus(mOrigin + mSize, mCapacity)]=aItem;
mSize++;
@ -260,7 +257,10 @@ nsDeque& nsDeque::PushFront(void* aItem) {
mOrigin--;
modasgn(mOrigin,mCapacity);
if (mSize==mCapacity) {
GrowCapacity();
if (!GrowCapacity()) {
NS_WARNING("out of memory");
return *this;
}
/* Comments explaining this are above*/
mData[mSize]=mData[mOrigin];
}

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

@ -233,7 +233,7 @@ private:
*/
nsDeque& operator=(const nsDeque& anOther);
PRInt32 GrowCapacity();
PRBool GrowCapacity();
};
/******************************************************