Bug 1200642 - Add OOM simulation to Vector r=Waldo
authorJon Coppeard <jcoppeard@mozilla.com>
Wed, 30 Sep 2015 11:34:48 +0100
changeset 300382 c508580bb56d3a4406fe46b8902cabc4d8477249
parent 300381 b75024c49a129410f8414f73cb4dfd914d0041ee
child 300383 61535c12904efe576ffb9bdfa77e7d2605869bdf
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1200642
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1200642 - Add OOM simulation to Vector r=Waldo
mfbt/Vector.h
--- a/mfbt/Vector.h
+++ b/mfbt/Vector.h
@@ -900,18 +900,24 @@ VectorBase<T, N, AP, TV>::initCapacity(s
   return true;
 }
 
 template<typename T, size_t N, class AP, class TV>
 inline bool
 VectorBase<T, N, AP, TV>::reserve(size_t aRequest)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
-  if (aRequest > mCapacity && MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
-    return false;
+  if (aRequest > mCapacity) {
+    if (MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
+      return false;
+    }
+  } else if (aRequest > N) {
+    if (!allocPolicy().checkSimulatedOOM()) {
+      return false;
+    }
   }
 #ifdef DEBUG
   if (aRequest > mReserved) {
     mReserved = aRequest;
   }
   MOZ_ASSERT(mLength <= mReserved);
   MOZ_ASSERT(mReserved <= mCapacity);
 #endif
@@ -928,18 +934,24 @@ VectorBase<T, N, AP, TV>::shrinkBy(size_
   mLength -= aIncr;
 }
 
 template<typename T, size_t N, class AP, class TV>
 MOZ_ALWAYS_INLINE bool
 VectorBase<T, N, AP, TV>::growBy(size_t aIncr)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
-  if (aIncr > mCapacity - mLength && MOZ_UNLIKELY(!growStorageBy(aIncr))) {
-    return false;
+  if (aIncr > mCapacity - mLength) {
+    if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
+      return false;
+    }
+  } else if (aIncr + mLength > N) {
+    if (!allocPolicy().checkSimulatedOOM()) {
+      return false;
+    }
   }
   MOZ_ASSERT(mLength + aIncr <= mCapacity);
   T* newend = endNoCheck() + aIncr;
   Impl::initialize(endNoCheck(), newend);
   mLength += aIncr;
 #ifdef DEBUG
   if (mLength > mReserved) {
     mReserved = mLength;
@@ -948,18 +960,24 @@ VectorBase<T, N, AP, TV>::growBy(size_t 
   return true;
 }
 
 template<typename T, size_t N, class AP, class TV>
 MOZ_ALWAYS_INLINE bool
 VectorBase<T, N, AP, TV>::growByUninitialized(size_t aIncr)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
-  if (aIncr > mCapacity - mLength && MOZ_UNLIKELY(!growStorageBy(aIncr))) {
-    return false;
+  if (aIncr > mCapacity - mLength) {
+    if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
+      return false;
+    }
+  } else if (aIncr + mLength > N) {
+    if (!allocPolicy().checkSimulatedOOM()) {
+      return false;
+    }
   }
   infallibleGrowByUninitialized(aIncr);
   return true;
 }
 
 template<typename T, size_t N, class AP, class TV>
 MOZ_ALWAYS_INLINE void
 VectorBase<T, N, AP, TV>::infallibleGrowByUninitialized(size_t aIncr)
@@ -1050,18 +1068,23 @@ VectorBase<T, N, AP, TV>::internalAppend
   ++mLength;
 }
 
 template<typename T, size_t N, class AP, class TV>
 MOZ_ALWAYS_INLINE bool
 VectorBase<T, N, AP, TV>::appendN(const T& aT, size_t aNeeded)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
-  if (mLength + aNeeded > mCapacity && MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
-    return false;
+  if (mLength + aNeeded > mCapacity) {
+    if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
+      return false;
+    }
+  } else if (mLength + aNeeded > N) {
+    if (!allocPolicy().checkSimulatedOOM())
+      return false;
   }
 #ifdef DEBUG
   if (mLength + aNeeded > mReserved) {
     mReserved = mLength + aNeeded;
   }
 #endif
   internalAppendN(aT, aNeeded);
   return true;
@@ -1132,18 +1155,23 @@ VectorBase<T, N, AP, TV>::erase(T* aBegi
 
 template<typename T, size_t N, class AP, class TV>
 template<typename U>
 MOZ_ALWAYS_INLINE bool
 VectorBase<T, N, AP, TV>::append(const U* aInsBegin, const U* aInsEnd)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
   size_t aNeeded = PointerRangeSize(aInsBegin, aInsEnd);
-  if (mLength + aNeeded > mCapacity && MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
-    return false;
+  if (mLength + aNeeded > mCapacity) {
+    if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
+      return false;
+    }
+  } else if (mLength + aNeeded > N) {
+    if (!allocPolicy().checkSimulatedOOM())
+      return false;
   }
 #ifdef DEBUG
   if (mLength + aNeeded > mReserved) {
     mReserved = mLength + aNeeded;
   }
 #endif
   internalAppend(aInsBegin, aNeeded);
   return true;
@@ -1161,18 +1189,23 @@ VectorBase<T, N, AP, TV>::internalAppend
 }
 
 template<typename T, size_t N, class AP, class TV>
 template<typename U>
 MOZ_ALWAYS_INLINE bool
 VectorBase<T, N, AP, TV>::append(U&& aU)
 {
   MOZ_REENTRANCY_GUARD_ET_AL;
-  if (mLength == mCapacity && MOZ_UNLIKELY(!growStorageBy(1))) {
-    return false;
+  if (mLength == mCapacity) {
+    if (MOZ_UNLIKELY(!growStorageBy(1))) {
+      return false;
+    }
+  } else if (mLength + 1 > N) {
+    if (!allocPolicy().checkSimulatedOOM())
+      return false;
   }
 #ifdef DEBUG
   if (mLength + 1 > mReserved) {
     mReserved = mLength + 1;
   }
 #endif
   internalAppend(Forward<U>(aU));
   return true;