From 53e2e5d8d6c6f550756ca2a9da9d7ce2d9c6482f Mon Sep 17 00:00:00 2001 From: FranklinDM Date: Mon, 27 Feb 2023 09:48:31 +0800 Subject: [PATCH] Issue #1375 - Part 1: Allow moving a reference into nsInterfaceHashtable --- xpcom/glue/nsInterfaceHashtable.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/xpcom/glue/nsInterfaceHashtable.h b/xpcom/glue/nsInterfaceHashtable.h index 54a5b79cd6..cdd36924ec 100644 --- a/xpcom/glue/nsInterfaceHashtable.h +++ b/xpcom/glue/nsInterfaceHashtable.h @@ -52,6 +52,21 @@ public: * @return The entry, or nullptr if not found. Do not release this pointer! */ Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const; + + /** + * Allows inserting a value into the hashtable, moving its owning reference + * count into the hashtable, avoiding an AddRef. + */ + void Put(KeyType aKey, already_AddRefed&& aData) + { + if (!Put(aKey, mozilla::Move(aData), mozilla::fallible)) { + NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount()); + } + } + + MOZ_MUST_USE bool Put(KeyType aKey, already_AddRefed&& aData, + const mozilla::fallible_t&); + using base_type::Put; }; template @@ -138,4 +153,19 @@ nsInterfaceHashtable::GetWeak(KeyType aKey, return nullptr; } +template +bool +nsInterfaceHashtable::Put(KeyType aKey, + already_AddRefed&& aValue, + const mozilla::fallible_t&) +{ + typename base_type::EntryType* ent = this->PutEntry(aKey); + if (!ent) { + return false; + } + + ent->mData = aValue; + return true; +} + #endif // nsInterfaceHashtable_h__