Bug 1371882 - MemoryBlockCache claims extra buffer capacity if any - r?cpearce draft
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 15 Jun 2017 18:38:52 +1200
changeset 595170 e3276ec587259bedda46b99500a65c7b9d5abed2
parent 595169 2994a8575637e5fb4c894221360c7868a5f1f9d1
child 595171 8cfd39d929c28cf06f3e01c75b27be01e21b57d2
push id64265
push usergsquelart@mozilla.com
push dateFri, 16 Jun 2017 03:37:56 +0000
reviewerscpearce
bugs1371882
milestone56.0a1
Bug 1371882 - MemoryBlockCache claims extra buffer capacity if any - r?cpearce MozReview-Commit-ID: GDLs8FcuD07
dom/media/MemoryBlockCache.cpp
--- a/dom/media/MemoryBlockCache.cpp
+++ b/dom/media/MemoryBlockCache.cpp
@@ -108,23 +108,35 @@ MemoryBlockCache::EnsureBufferCanContain
     LOG("EnsureBufferCanContain(%zu) - buffer size %zu, wanted + %zu = %zu, "
         "allocation failed",
         aContentLength,
         initialLength,
         extra,
         desiredLength);
     return false;
   }
-  size_t newSizes = static_cast<size_t>(mCombinedSizes += extra);
-  LOG("EnsureBufferCanContain(%zu) - buffer size %zu + %zu = %zu; combined "
+  MOZ_ASSERT(mBuffer.Length() == desiredLength);
+  const size_t capacity = mBuffer.Capacity();
+  const size_t extraCapacity = capacity - desiredLength;
+  if (extraCapacity != 0) {
+    // Our buffer was given a larger capacity than the requested length, we may
+    // as well claim that extra capacity, both for our accounting, and to
+    // possibly bypass some future growths that would fit in this new capacity.
+    mBuffer.SetLength(capacity);
+  }
+  size_t newSizes =
+    static_cast<size_t>(mCombinedSizes += (extra + extraCapacity));
+  LOG("EnsureBufferCanContain(%zu) - buffer size %zu + requested %zu + bonus "
+      "%zu = %zu; combined "
       "sizes %zu",
       aContentLength,
       initialLength,
       extra,
-      mBuffer.Length(),
+      extraCapacity,
+      capacity,
       newSizes);
   mHasGrown = true;
   return true;
 }
 
 nsresult
 MemoryBlockCache::Init()
 {