From 59203c63bb777a3bacec32fb8830fba33540e809 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:35:29 +0200 Subject: Adding upstream version 127.0. Signed-off-by: Daniel Baumann --- dom/media/platforms/SimpleMap.h | 86 +++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 17 deletions(-) (limited to 'dom/media/platforms/SimpleMap.h') diff --git a/dom/media/platforms/SimpleMap.h b/dom/media/platforms/SimpleMap.h index c26bff1e9a..635ba6f085 100644 --- a/dom/media/platforms/SimpleMap.h +++ b/dom/media/platforms/SimpleMap.h @@ -5,49 +5,101 @@ #ifndef mozilla_SimpleMap_h #define mozilla_SimpleMap_h +#include + +#include "mozilla/Maybe.h" #include "mozilla/Mutex.h" #include "nsTArray.h" -#include - namespace mozilla { -template +struct ThreadSafePolicy { + struct PolicyLock { + explicit PolicyLock(const char* aName) : mMutex(aName) {} + Mutex mMutex MOZ_UNANNOTATED; + }; + PolicyLock& mPolicyLock; + explicit ThreadSafePolicy(PolicyLock& aPolicyLock) + : mPolicyLock(aPolicyLock) { + mPolicyLock.mMutex.Lock(); + } + ~ThreadSafePolicy() { mPolicyLock.mMutex.Unlock(); } +}; + +struct NoOpPolicy { + struct PolicyLock { + explicit PolicyLock(const char*) {} + }; + explicit NoOpPolicy(PolicyLock&) {} + ~NoOpPolicy() = default; +}; + +// An map employing an array instead of a hash table to optimize performance, +// particularly beneficial when the number of expected items in the map is +// small. +template class SimpleMap { - public: - typedef std::pair Element; + using ElementType = std::pair; + using MapType = AutoTArray; - SimpleMap() : mMutex("SimpleMap") {} + public: + SimpleMap() : mLock("SimpleMap"){}; + // Check if aKey is in the map. + bool Contains(const K& aKey) { + struct Comparator { + bool Equals(const ElementType& aElement, const K& aKey) const { + return aElement.first == aKey; + } + }; + Policy guard(mLock); + return mMap.Contains(aKey, Comparator()); + } // Insert Key and Value pair at the end of our map. - void Insert(int64_t aKey, const T& aValue) { - MutexAutoLock lock(mMutex); + void Insert(const K& aKey, const V& aValue) { + Policy guard(mLock); mMap.AppendElement(std::make_pair(aKey, aValue)); } // Sets aValue matching aKey and remove it from the map if found. // The element returned is the first one found. // Returns true if found, false otherwise. - bool Find(int64_t aKey, T& aValue) { - MutexAutoLock lock(mMutex); + bool Find(const K& aKey, V& aValue) { + if (Maybe v = Take(aKey)) { + aValue = v.extract(); + return true; + } + return false; + } + // Take the value matching aKey and remove it from the map if found. + Maybe Take(const K& aKey) { + Policy guard(mLock); for (uint32_t i = 0; i < mMap.Length(); i++) { - Element& element = mMap[i]; + ElementType& element = mMap[i]; if (element.first == aKey) { - aValue = element.second; + Maybe value = Some(element.second); mMap.RemoveElementAt(i); - return true; + return value; } } - return false; + return Nothing(); } // Remove all elements of the map. void Clear() { - MutexAutoLock lock(mMutex); + Policy guard(mLock); mMap.Clear(); } + // Iterate through all elements of the map and call the function F. + template + void ForEach(F&& aCallback) { + Policy guard(mLock); + for (const auto& element : mMap) { + aCallback(element.first, element.second); + } + } private: - Mutex mMutex MOZ_UNANNOTATED; // To protect mMap. - AutoTArray mMap; + typename Policy::PolicyLock mLock; + MapType mMap; }; } // namespace mozilla -- cgit v1.2.3