DDLifetimes.cpp (2898B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "DDLifetimes.h" 8 9 #include "DDLogUtils.h" 10 11 namespace mozilla { 12 13 DDLifetime* DDLifetimes::FindLifetime(const DDLogObject& aObject, 14 const DDMessageIndex& aIndex) { 15 LifetimesForObject* lifetimes = mLifetimes.Get(aObject); 16 if (lifetimes) { 17 for (DDLifetime& lifetime : *lifetimes) { 18 if (lifetime.mObject == aObject && lifetime.IsAliveAt(aIndex)) { 19 return &lifetime; 20 } 21 } 22 } 23 return nullptr; 24 } 25 26 const DDLifetime* DDLifetimes::FindLifetime( 27 const DDLogObject& aObject, const DDMessageIndex& aIndex) const { 28 const LifetimesForObject* lifetimes = mLifetimes.Get(aObject); 29 if (lifetimes) { 30 for (const DDLifetime& lifetime : *lifetimes) { 31 if (lifetime.mObject == aObject && lifetime.IsAliveAt(aIndex)) { 32 return &lifetime; 33 } 34 } 35 } 36 return nullptr; 37 } 38 39 DDLifetime& DDLifetimes::CreateLifetime( 40 const DDLogObject& aObject, DDMessageIndex aIndex, 41 const DDTimeStamp& aConstructionTimeStamp) { 42 // Use negative tags for yet-unclassified messages. 43 static int32_t sTag = 0; 44 if (--sTag > 0) { 45 sTag = -1; 46 } 47 LifetimesForObject* lifetimes = mLifetimes.GetOrInsertNew(aObject, 1); 48 DDLifetime& lifetime = *lifetimes->AppendElement( 49 DDLifetime(aObject, aIndex, aConstructionTimeStamp, sTag)); 50 return lifetime; 51 } 52 53 void DDLifetimes::RemoveLifetime(const DDLifetime* aLifetime) { 54 LifetimesForObject* lifetimes = mLifetimes.Get(aLifetime->mObject); 55 MOZ_ASSERT(lifetimes); 56 DDL_LOG(aLifetime->mMediaElement ? mozilla::LogLevel::Debug 57 : mozilla::LogLevel::Warning, 58 "Remove lifetime %s", aLifetime->Printf().get()); 59 // We should have been given a pointer inside this lifetimes array. 60 auto arrayIndex = aLifetime - lifetimes->Elements(); 61 MOZ_ASSERT(static_cast<size_t>(arrayIndex) < lifetimes->Length()); 62 lifetimes->RemoveElementAt(arrayIndex); 63 } 64 65 void DDLifetimes::RemoveLifetimesFor( 66 const dom::HTMLMediaElement* aMediaElement) { 67 for (const auto& lifetimes : mLifetimes.Values()) { 68 lifetimes->RemoveElementsBy([aMediaElement](const DDLifetime& lifetime) { 69 return lifetime.mMediaElement == aMediaElement; 70 }); 71 } 72 } 73 74 void DDLifetimes::Clear() { mLifetimes.Clear(); } 75 76 size_t DDLifetimes::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { 77 size_t size = mLifetimes.ShallowSizeOfExcludingThis(aMallocSizeOf); 78 for (const auto& lifetimes : mLifetimes.Values()) { 79 size += lifetimes->ShallowSizeOfExcludingThis(aMallocSizeOf); 80 } 81 return size; 82 } 83 84 } // namespace mozilla