Add nsVoidArray::FastElementAt that requires the caller to do bounds checks (0 <= aIndex < Count()). Make ElementAt a little safer without loss of speed (back to the old safety). Make nsCOMArray<T>::ObjectAt use FastElementAt. b=96108 r=darin

This commit is contained in:
dbaron%dbaron.org 2004-09-14 17:17:21 +00:00
Родитель 8f66abf599
Коммит 12063cbd2c
2 изменённых файлов: 15 добавлений и 13 удалений

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

@ -97,7 +97,7 @@ public:
}
nsISupports* ObjectAt(PRInt32 aIndex) const {
return NS_STATIC_CAST(nsISupports*, mArray.ElementAt(aIndex));
return NS_STATIC_CAST(nsISupports*, mArray.FastElementAt(aIndex));
}
nsISupports* SafeObjectAt(PRInt32 aIndex) const {

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

@ -66,28 +66,30 @@ public:
return mImpl ? (PRInt32(mImpl->mBits) & kArraySizeMask) : 0;
}
void* FastElementAt(PRInt32 aIndex) const
{
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "index out of range");
return mImpl->mArray[aIndex];
}
// This both asserts and bounds-checks, because (1) we don't want
// people to write bad code, but (2) we don't want to change it to
// crashing for backwards compatibility. See bug 96108.
void* ElementAt(PRInt32 aIndex) const
{
NS_ASSERTION(aIndex >= 0,"nsVoidArray::ElementAt(negative index) - note on bug 96108");
NS_ASSERTION(aIndex < Count(),"nsVoidArray::ElementAt(index past end array) - note on bug 96108");
// This will go away once all assertions are handled and we feel
// comfortable that there aren't any more out there. Negative values
// are all handled I think.
if (aIndex >= Count())
{
return nsnull;
}
return mImpl ? mImpl->mArray[aIndex] : nsnull;
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "index out of range");
return SafeElementAt(aIndex);
}
// bounds-checked version
void* SafeElementAt(PRInt32 aIndex) const
{
if (aIndex < 0 || aIndex >= Count())
if (PRUint32(aIndex) >= PRUint32(Count())) // handles aIndex < 0 too
{
return nsnull;
}
return mImpl ? mImpl->mArray[aIndex] : nsnull;
// The bounds check ensures mImpl is non-null.
return mImpl->mArray[aIndex];
}
void* operator[](PRInt32 aIndex) const { return ElementAt(aIndex); }