diff --git a/dom/base/ImageTracker.cpp b/dom/base/ImageTracker.cpp index 9fef059bcaf3..a78ec20b0109 100644 --- a/dom/base/ImageTracker.cpp +++ b/dom/base/ImageTracker.cpp @@ -28,26 +28,27 @@ ImageTracker::Add(imgIRequest* aImage) { MOZ_ASSERT(aImage); - // See if the image is already in the hashtable. If it is, get the old count. - uint32_t oldCount = 0; - mImages.Get(aImage, &oldCount); - - // Put the image in the hashtable, with the proper count. - mImages.Put(aImage, oldCount + 1); - nsresult rv = NS_OK; + auto entry = mImages.LookupForAdd(aImage); + if (entry) { + // The image is already in the hashtable. Increment its count. + uint32_t oldCount = entry.Data(); + MOZ_ASSERT(oldCount > 0, "Entry in the image tracker with count 0!"); + entry.Data() = oldCount + 1; + } else { + // A new entry was inserted - set the count to 1. + entry.OrInsert([]() { return 1; }); - // If this is the first insertion and we're locking images, lock this image - // too. - if (oldCount == 0 && mLocking) { - rv = aImage->LockImage(); - } + // If we're locking images, lock this image too. + if (mLocking) { + rv = aImage->LockImage(); + } - // If this is the first insertion and we're animating images, request - // that this image be animated too. - if (oldCount == 0 && mAnimating) { - nsresult rv2 = aImage->IncrementAnimationConsumers(); - rv = NS_SUCCEEDED(rv) ? rv2 : rv; + // If we're animating images, request that this image be animated too. + if (mAnimating) { + nsresult rv2 = aImage->IncrementAnimationConsumers(); + rv = NS_SUCCEEDED(rv) ? rv2 : rv; + } } return rv; @@ -59,23 +60,23 @@ ImageTracker::Remove(imgIRequest* aImage, uint32_t aFlags) NS_ENSURE_ARG_POINTER(aImage); // Get the old count. It should exist and be > 0. - uint32_t count = 0; - DebugOnly found = mImages.Get(aImage, &count); + DebugOnly found = false; + bool remove = false; + mImages.LookupRemoveIf(aImage, + [&found, &remove] (uint32_t& aCount) { + found = true; + MOZ_ASSERT(aCount > 0, "Entry in the image tracker with count 0!"); + --aCount; + // If the count is now zero, remove it from the tracker. + remove = aCount == 0; + return remove; + }); + MOZ_ASSERT(found, "Removing image that wasn't in the tracker!"); - MOZ_ASSERT(count > 0, "Entry in the cache tracker with count 0!"); - - // We're removing, so decrement the count. - count--; - - // If the count is now zero, remove from the tracker. - // Otherwise, set the new value. - if (count != 0) { - mImages.Put(aImage, count); + if (!remove) { return NS_OK; } - mImages.Remove(aImage); - nsresult rv = NS_OK; // Now that we're no longer tracking this image, unlock it if we'd