diff --git a/xpcom/glue/nsInterfaceHashtable.h b/xpcom/glue/nsInterfaceHashtable.h index 14368af63..fbd5c9238 100644 --- a/xpcom/glue/nsInterfaceHashtable.h +++ b/xpcom/glue/nsInterfaceHashtable.h @@ -53,6 +53,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 @@ -139,4 +154,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__