Add nsTArray<E>::TruncateLength, which is like SetLength, except only allows shortening of the array. (Bug 191448) r=bsmedberg
authorL. David Baron <dbaron@dbaron.org>
Tue, 06 Jan 2009 15:21:00 -0800
changeset 23392 63fc1585fe0070724e339c277480681358b0b23b
parent 23391 7f7c7c7a4afee6323bae1510c851e152ca4a729b
child 23393 c162278affadeb185e5648d5d53d5eedfe8d1995
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs191448
milestone1.9.2a1pre
Add nsTArray<E>::TruncateLength, which is like SetLength, except only allows shortening of the array. (Bug 191448) r=bsmedberg
layout/generic/nsFloatManager.cpp
xpcom/glue/nsTArray.h
--- a/layout/generic/nsFloatManager.cpp
+++ b/layout/generic/nsFloatManager.cpp
@@ -280,17 +280,17 @@ nsFloatManager::RemoveTrailingRegions(ns
 
   PRUint32 newLength = mFloats.Length();
   while (newLength > 0) {
     if (!frameSet.Contains(mFloats[newLength - 1].mFrame)) {
       break;
     }
     --newLength;
   }
-  mFloats.RemoveElementsAt(newLength, mFloats.Length() - newLength);
+  mFloats.TruncateLength(newLength);
 
 #ifdef DEBUG
   for (PRUint32 i = 0; i < mFloats.Length(); ++i) {
     NS_ASSERTION(!frameSet.Contains(mFloats[i].mFrame),
                  "Frame region deletion was requested but we couldn't delete it");
   }
 #endif
 
@@ -329,18 +329,17 @@ nsFloatManager::PopState(SavedState* aSt
 {
   NS_PRECONDITION(aState, "No state to restore?");
 
   mX = aState->mX;
   mY = aState->mY;
 
   NS_ASSERTION(aState->mFloatInfoCount <= mFloats.Length(),
                "somebody misused PushState/PopState");
-  mFloats.RemoveElementsAt(aState->mFloatInfoCount,
-                           mFloats.Length() - aState->mFloatInfoCount);
+  mFloats.TruncateLength(aState->mFloatInfoCount);
 }
 
 nscoord
 nsFloatManager::GetLowestFloatTop() const
 {
   if (!HasAnyFloats()) {
     return nscoord_MIN;
   }
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -655,24 +655,39 @@ class nsTArray : public nsTArray_base {
     }
 
     // This method modifies the length of the array.  If the new length is
     // larger than the existing length of the array, then new elements will be
     // constructed using elem_type's default constructor.  Otherwise, this call
     // removes elements from the array (see also RemoveElementsAt).
     // @param newLen  The desired length of this array.
     // @return        True if the operation succeeded; false otherwise.
+    // See also TruncateLength if the new length is guaranteed to be
+    // smaller than the old.
     PRBool SetLength(size_type newLen) {
       size_type oldLen = Length();
       if (newLen > oldLen) {
         return InsertElementsAt(oldLen, newLen - oldLen) != nsnull;
       }
       
+      TruncateLength(newLen);
+      return PR_TRUE;
+    }
+
+    // This method modifies the length of the array, but may only be
+    // called when the new length is shorter than the old.  It can
+    // therefore be called when elem_type has no default constructor,
+    // unlike SetLength.  It removes elements from the array (see also
+    // RemoveElementsAt).
+    // @param newLen  The desired length of this array.
+    void TruncateLength(size_type newLen) {
+      size_type oldLen = Length();
+      NS_ABORT_IF_FALSE(newLen <= oldLen,
+                        "caller should use SetLength instead");
       RemoveElementsAt(newLen, oldLen - newLen);
-      return PR_TRUE;
     }
 
     // This method inserts elements into the array, constructing
     // them using elem_type's default constructor.
     // @param index the place to insert the new elements. This must be no
     //              greater than the current length of the array.
     // @param count the number of elements to insert
     elem_type *InsertElementsAt(index_type index, size_type count) {