Bug 1634281 - Merge nsRefPtrHashtable and nsInterfaceHashtable into nsRefCountedHashtable. r=xpcom-reviewers,necko-reviewers,nika

The only difference between nsRefPtrHashtable and nsInterfaceHashtable was
that the former enforced explicit refcounting also with InsertOrUpdate.
This enforcement is removed.

Differential Revision: https://phabricator.services.mozilla.com/D106006
This commit is contained in:
Simon Giesecke 2021-03-04 18:52:19 +00:00
Родитель 3004f1f2f3
Коммит c57f081813
5 изменённых файлов: 274 добавлений и 364 удалений

Просмотреть файл

@ -66,6 +66,7 @@ EXPORTS += [
"nsPointerHashKeys.h",
"nsProperties.h",
"nsQuickSort.h",
"nsRefCountedHashtable.h",
"nsRefPtrHashtable.h",
"nsSimpleEnumerator.h",
"nsStaticAtomUtils.h",

Просмотреть файл

@ -11,6 +11,12 @@
struct PLDHashEntryHdr;
template <class T>
class MOZ_IS_REFPTR nsCOMPtr;
template <class T>
class MOZ_IS_REFPTR RefPtr;
template <class EntryType>
class MOZ_NEEDS_NO_VTABLE_TYPE nsTHashtable;
@ -35,10 +41,30 @@ class nsClassHashtable;
template <class KeyClass, class DataType>
using nsDataHashtable = nsBaseHashtable<KeyClass, DataType, DataType>;
template <class KeyClass, class Interface>
class nsInterfaceHashtable;
template <class KeyClass, class PtrType>
class nsRefPtrHashtable;
class nsRefCountedHashtable;
/**
* templated hashtable class maps keys to interface pointers.
* See nsBaseHashtable for complete declaration.
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
* for a complete specification.
* @param Interface the interface-type being wrapped
* @see nsDataHashtable, nsClassHashtable
*/
template <class KeyClass, class Interface>
using nsInterfaceHashtable =
nsRefCountedHashtable<KeyClass, nsCOMPtr<Interface>>;
/**
* templated hashtable class maps keys to reference pointers.
* See nsBaseHashtable for complete declaration.
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
* for a complete specification.
* @param PtrType the reference-type being wrapped
* @see nsDataHashtable, nsClassHashtable
*/
template <class KeyClass, class ClassType>
using nsRefPtrHashtable = nsRefCountedHashtable<KeyClass, RefPtr<ClassType>>;
#endif // XPCOM_DS_NSHASHTABLESFWD_H_

Просмотреть файл

@ -7,183 +7,8 @@
#ifndef nsInterfaceHashtable_h__
#define nsInterfaceHashtable_h__
#include "nsBaseHashtable.h"
#include "nsRefCountedHashtable.h"
#include "nsHashKeys.h"
#include "nsCOMPtr.h"
/**
* templated hashtable class maps keys to interface pointers.
* See nsBaseHashtable for complete declaration.
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
* for a complete specification.
* @param Interface the interface-type being wrapped
* @see nsDataHashtable, nsClassHashtable
*/
template <class KeyClass, class Interface>
class nsInterfaceHashtable
: public nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> {
public:
typedef typename KeyClass::KeyType KeyType;
typedef Interface* UserDataType;
typedef nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> base_type;
nsInterfaceHashtable() = default;
explicit nsInterfaceHashtable(uint32_t aInitLength)
: nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>(
aInitLength) {}
/**
* @copydoc nsBaseHashtable::Get
* @param aData This is an XPCOM getter, so aData is already_addrefed.
* If the key doesn't exist, aData will be set to nullptr.
*/
bool Get(KeyType aKey, UserDataType* aData) const;
/**
* @copydoc nsBaseHashtable::Get
*/
[[nodiscard]] already_AddRefed<Interface> Get(KeyType aKey) const;
/**
* Gets a weak reference to the hashtable entry.
* @param aFound If not nullptr, will be set to true if the entry is found,
* to false otherwise.
* @return The entry, or nullptr if not found. Do not release this pointer!
*/
[[nodiscard]] 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 InsertOrUpdate(KeyType aKey, already_AddRefed<Interface>&& aData) {
if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
}
}
[[nodiscard]] bool InsertOrUpdate(KeyType aKey,
already_AddRefed<Interface>&& aData,
const mozilla::fallible_t&);
using base_type::InsertOrUpdate;
/**
* Remove the entry associated with aKey (if any), optionally _moving_ its
* current value into *aData, thereby avoiding calls to AddRef and Release.
* Return true if found.
* @param aKey the key to remove from the hashtable
* @param aData where to move the value (if non-null). If an entry is not
* found it will be set to nullptr.
* @return true if an entry for aKey was found (and removed)
*/
inline bool Remove(KeyType aKey, Interface** aData = nullptr);
};
template <typename K, typename T>
inline void ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField) {
aField.Clear();
}
template <typename K, typename T>
inline void ImplCycleCollectionTraverse(
nsCycleCollectionTraversalCallback& aCallback,
const nsInterfaceHashtable<K, T>& aField, const char* aName,
uint32_t aFlags = 0) {
for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
}
}
//
// nsInterfaceHashtable definitions
//
template <class KeyClass, class Interface>
bool nsInterfaceHashtable<KeyClass, Interface>::Get(
KeyType aKey, UserDataType* aInterface) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aInterface) {
*aInterface = ent->GetData();
NS_IF_ADDREF(*aInterface);
}
return true;
}
// if the key doesn't exist, set *aInterface to null
// so that it is a valid XPCOM getter
if (aInterface) {
*aInterface = nullptr;
}
return false;
}
template <class KeyClass, class Interface>
already_AddRefed<Interface> nsInterfaceHashtable<KeyClass, Interface>::Get(
KeyType aKey) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return nullptr;
}
nsCOMPtr<Interface> copy = ent->GetData();
return copy.forget();
}
template <class KeyClass, class Interface>
Interface* nsInterfaceHashtable<KeyClass, Interface>::GetWeak(
KeyType aKey, bool* aFound) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aFound) {
*aFound = true;
}
return ent->GetData();
}
// Key does not exist, return nullptr and set aFound to false
if (aFound) {
*aFound = false;
}
return nullptr;
}
template <class KeyClass, class Interface>
bool nsInterfaceHashtable<KeyClass, Interface>::InsertOrUpdate(
KeyType aKey, already_AddRefed<Interface>&& aValue,
const mozilla::fallible_t&) {
typename base_type::EntryType* ent = this->PutEntry(aKey);
if (!ent) {
return false;
}
ent->SetData(std::move(aValue));
return true;
}
template <class KeyClass, class Interface>
bool nsInterfaceHashtable<KeyClass, Interface>::Remove(KeyType aKey,
Interface** aData) {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aData) {
ent->GetModifiableData()->forget(aData);
}
this->RemoveEntry(ent);
return true;
}
if (aData) {
*aData = nullptr;
}
return false;
}
#endif // nsInterfaceHashtable_h__

Просмотреть файл

@ -0,0 +1,241 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_
#define XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_
#include "nsBaseHashtable.h"
#include "nsHashKeys.h"
/**
* templated hashtable class maps keys to reference pointers.
* See nsBaseHashtable for complete declaration.
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
* for a complete specification.
* @param PtrType the reference-type being wrapped
* @see nsDataHashtable, nsClassHashtable
*/
template <class KeyClass, class PtrType>
class nsRefCountedHashtable
: public nsBaseHashtable<
KeyClass, PtrType,
typename mozilla::detail::SmartPtrTraits<PtrType>::RawPointerType> {
public:
using KeyType = typename KeyClass::KeyType;
using SmartPtrTraits = mozilla::detail::SmartPtrTraits<PtrType>;
using PointeeType = typename SmartPtrTraits::PointeeType;
using RawPointerType = typename SmartPtrTraits::RawPointerType;
using base_type = nsBaseHashtable<KeyClass, PtrType, RawPointerType>;
using base_type::base_type;
static_assert(SmartPtrTraits::IsRefCounted);
/**
* @copydoc nsBaseHashtable::Get
* @param aData This is an XPCOM getter, so aData is already_addrefed.
* If the key doesn't exist, *aData will be set to nullptr.
*/
bool Get(KeyType aKey, RawPointerType* aData) const;
/**
* @copydoc nsBaseHashtable::Get
*/
[[nodiscard]] already_AddRefed<PointeeType> Get(KeyType aKey) const;
/**
* Gets a weak reference to the hashtable entry.
* @param aFound If not nullptr, will be set to true if the entry is found,
* to false otherwise.
* @return The entry, or nullptr if not found. Do not release this pointer!
*/
[[nodiscard]] RawPointerType GetWeak(KeyType aKey,
bool* aFound = nullptr) const;
using base_type::InsertOrUpdate;
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
void InsertOrUpdate(
KeyType aKey,
typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData);
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
[[nodiscard]] bool InsertOrUpdate(
KeyType aKey,
typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData,
const mozilla::fallible_t&);
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
void InsertOrUpdate(KeyType aKey, already_AddRefed<U>&& aData);
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
[[nodiscard]] bool InsertOrUpdate(KeyType aKey, already_AddRefed<U>&& aData,
const mozilla::fallible_t&);
/**
* Remove the entry associated with aKey (if any), optionally _moving_ its
* current value into *aData, thereby avoiding calls to AddRef and Release.
* Return true if found.
* @param aKey the key to remove from the hashtable
* @param aData where to move the value (if non-null). If an entry is not
* found it will be set to nullptr.
* @return true if an entry for aKey was found (and removed)
*/
inline bool Remove(KeyType aKey, RawPointerType* aData = nullptr);
};
template <typename K, typename T>
inline void ImplCycleCollectionUnlink(nsRefCountedHashtable<K, T>& aField) {
aField.Clear();
}
template <typename K, typename T>
inline void ImplCycleCollectionTraverse(
nsCycleCollectionTraversalCallback& aCallback,
nsRefCountedHashtable<K, T>& aField, const char* aName,
uint32_t aFlags = 0) {
for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
}
}
//
// nsRefCountedHashtable definitions
//
template <class KeyClass, class PtrType>
bool nsRefCountedHashtable<KeyClass, PtrType>::Get(
KeyType aKey, RawPointerType* aRefPtr) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aRefPtr) {
*aRefPtr = ent->GetData();
NS_IF_ADDREF(*aRefPtr);
}
return true;
}
// if the key doesn't exist, set *aRefPtr to null
// so that it is a valid XPCOM getter
if (aRefPtr) {
*aRefPtr = nullptr;
}
return false;
}
template <class KeyClass, class PtrType>
already_AddRefed<typename nsRefCountedHashtable<KeyClass, PtrType>::PointeeType>
nsRefCountedHashtable<KeyClass, PtrType>::Get(KeyType aKey) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return nullptr;
}
PtrType copy = ent->GetData();
return copy.forget();
}
template <class KeyClass, class PtrType>
typename nsRefCountedHashtable<KeyClass, PtrType>::RawPointerType
nsRefCountedHashtable<KeyClass, PtrType>::GetWeak(KeyType aKey,
bool* aFound) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aFound) {
*aFound = true;
}
return ent->GetData();
}
// Key does not exist, return nullptr and set aFound to false
if (aFound) {
*aFound = false;
}
return nullptr;
}
template <class KeyClass, class PtrType>
template <typename U, typename>
void nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
KeyType aKey,
typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData) {
if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
}
}
template <class KeyClass, class PtrType>
template <typename U, typename>
bool nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
KeyType aKey,
typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData,
const mozilla::fallible_t&) {
typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);
if (!ent) {
return false;
}
ent->SetData(std::move(aData));
return true;
}
template <class KeyClass, class PtrType>
template <typename U, typename>
void nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
KeyType aKey, already_AddRefed<U>&& aData) {
if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
}
}
template <class KeyClass, class PtrType>
template <typename U, typename>
bool nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
KeyType aKey, already_AddRefed<U>&& aData, const mozilla::fallible_t&) {
typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);
if (!ent) {
return false;
}
ent->SetData(std::move(aData));
return true;
}
template <class KeyClass, class PtrType>
bool nsRefCountedHashtable<KeyClass, PtrType>::Remove(KeyType aKey,
RawPointerType* aRefPtr) {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aRefPtr) {
ent->GetModifiableData()->forget(aRefPtr);
}
this->RemoveEntry(ent);
return true;
}
if (aRefPtr) {
*aRefPtr = nullptr;
}
return false;
}
#endif // XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_

Просмотреть файл

@ -7,189 +7,6 @@
#ifndef nsRefPtrHashtable_h__
#define nsRefPtrHashtable_h__
#include "nsBaseHashtable.h"
#include "nsHashKeys.h"
/**
* templated hashtable class maps keys to reference pointers.
* See nsBaseHashtable for complete declaration.
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
* for a complete specification.
* @param PtrType the reference-type being wrapped
* @see nsDataHashtable, nsClassHashtable
*/
template <class KeyClass, class PtrType>
class nsRefPtrHashtable
: public nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*> {
public:
typedef typename KeyClass::KeyType KeyType;
typedef PtrType* UserDataType;
typedef nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*> base_type;
nsRefPtrHashtable() = default;
explicit nsRefPtrHashtable(uint32_t aInitLength)
: nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*>(aInitLength) {}
/**
* @copydoc nsBaseHashtable::Get
* @param aData This is an XPCOM getter, so aData is already_addrefed.
* If the key doesn't exist, *aData will be set to nullptr.
*/
bool Get(KeyType aKey, UserDataType* aData) const;
/**
* @copydoc nsBaseHashtable::Get
*/
[[nodiscard]] already_AddRefed<PtrType> Get(KeyType aKey) const;
/**
* Gets a weak reference to the hashtable entry.
* @param aFound If not nullptr, will be set to true if the entry is found,
* to false otherwise.
* @return The entry, or nullptr if not found. Do not release this pointer!
*/
[[nodiscard]] PtrType* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
// Hide base class' InsertOrUpdate overloads intentionally, to make any
// necessary refcounting explicit when calling InsertOrUpdate.
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PtrType, U>>>
void InsertOrUpdate(KeyType aKey, RefPtr<U>&& aData);
template <typename U,
typename = std::enable_if_t<std::is_base_of_v<PtrType, U>>>
[[nodiscard]] bool InsertOrUpdate(KeyType aKey, RefPtr<U>&& aData,
const mozilla::fallible_t&);
/**
* Remove the entry associated with aKey (if any), optionally _moving_ its
* current value into *aData, thereby avoiding calls to AddRef and Release.
* Return true if found.
* @param aKey the key to remove from the hashtable
* @param aData where to move the value (if non-null). If an entry is not
* found it will be set to nullptr.
* @return true if an entry for aKey was found (and removed)
*/
inline bool Remove(KeyType aKey, UserDataType* aData = nullptr);
};
template <typename K, typename T>
inline void ImplCycleCollectionUnlink(nsRefPtrHashtable<K, T>& aField) {
aField.Clear();
}
template <typename K, typename T>
inline void ImplCycleCollectionTraverse(
nsCycleCollectionTraversalCallback& aCallback,
nsRefPtrHashtable<K, T>& aField, const char* aName, uint32_t aFlags = 0) {
for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
}
}
//
// nsRefPtrHashtable definitions
//
template <class KeyClass, class PtrType>
bool nsRefPtrHashtable<KeyClass, PtrType>::Get(KeyType aKey,
UserDataType* aRefPtr) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aRefPtr) {
*aRefPtr = ent->GetData();
NS_IF_ADDREF(*aRefPtr);
}
return true;
}
// if the key doesn't exist, set *aRefPtr to null
// so that it is a valid XPCOM getter
if (aRefPtr) {
*aRefPtr = nullptr;
}
return false;
}
template <class KeyClass, class PtrType>
already_AddRefed<PtrType> nsRefPtrHashtable<KeyClass, PtrType>::Get(
KeyType aKey) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return nullptr;
}
RefPtr<PtrType> copy = ent->GetData();
return copy.forget();
}
template <class KeyClass, class PtrType>
PtrType* nsRefPtrHashtable<KeyClass, PtrType>::GetWeak(KeyType aKey,
bool* aFound) const {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aFound) {
*aFound = true;
}
return ent->GetData();
}
// Key does not exist, return nullptr and set aFound to false
if (aFound) {
*aFound = false;
}
return nullptr;
}
template <class KeyClass, class PtrType>
template <typename U, typename>
void nsRefPtrHashtable<KeyClass, PtrType>::InsertOrUpdate(KeyType aKey,
RefPtr<U>&& aData) {
if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
}
}
template <class KeyClass, class PtrType>
template <typename U, typename>
bool nsRefPtrHashtable<KeyClass, PtrType>::InsertOrUpdate(
KeyType aKey, RefPtr<U>&& aData, const mozilla::fallible_t&) {
typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);
if (!ent) {
return false;
}
ent->SetData(std::move(aData));
return true;
}
template <class KeyClass, class PtrType>
bool nsRefPtrHashtable<KeyClass, PtrType>::Remove(KeyType aKey,
UserDataType* aRefPtr) {
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (ent) {
if (aRefPtr) {
ent->GetModifiableData()->forget(aRefPtr);
}
this->RemoveEntry(ent);
return true;
}
if (aRefPtr) {
*aRefPtr = nullptr;
}
return false;
}
#include "nsRefCountedHashtable.h"
#endif // nsRefPtrHashtable_h__