Bug 1849386 - Use consistent pointer hashing. r=nika,xpcom-reviewers

* DefaultHasher<T*> used HashGeneric<size_t>
 * nsPtrHashKey<T> used HashGeneric<T*>
 * nsRefPtrHashKey<T> used NS_PTR_TO_UINT32 >> 2

Make sure to always use HashGeneric<T*> for these.

Remove unused / redundant hash keys.

Differential Revision: https://phabricator.services.mozilla.com/D186553
This commit is contained in:
Emilio Cobos Álvarez 2023-08-18 22:35:40 +00:00
Родитель 40713b0f38
Коммит 9b5ea61bed
5 изменённых файлов: 20 добавлений и 44 удалений

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

@ -752,10 +752,7 @@ template <typename Key>
struct PointerHasher {
using Lookup = Key;
static HashNumber hash(const Lookup& aLookup) {
size_t word = reinterpret_cast<size_t>(aLookup);
return HashGeneric(word);
}
static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); }
static bool match(const Key& aKey, const Lookup& aLookup) {
return aKey == aLookup;

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

@ -456,8 +456,7 @@ class CycleCollectedJSRuntime {
JSHolderMap mJSHolders;
Maybe<JSHolderMap::Iter> mHolderIter;
typedef nsTHashMap<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*>
DeferredFinalizerTable;
using DeferredFinalizerTable = nsTHashMap<DeferredFinalizeFunction, void*>;
DeferredFinalizerTable mDeferredFinalizerTable;
RefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable;

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

@ -53,9 +53,7 @@ inline uint32_t HashString(const nsACString& aStr) {
* nsFloatHashKey
* IntPtrHashKey
* nsPtrHashKey
* nsClearingPtrHashKey
* nsVoidPtrHashKey
* nsClearingVoidPtrHashKey
* nsISupportsHashKey
* nsIDHashKey
* nsDepCharHashKey
@ -314,14 +312,12 @@ using IntPtrHashKey = nsIntegralHashKey<intptr_t>;
*/
class nsISupportsHashKey : public PLDHashEntryHdr {
public:
typedef nsISupports* KeyType;
typedef const nsISupports* KeyTypePointer;
using KeyType = nsISupports*;
using KeyTypePointer = const nsISupports*;
explicit nsISupportsHashKey(const nsISupports* aKey)
: mSupports(const_cast<nsISupports*>(aKey)) {}
nsISupportsHashKey(nsISupportsHashKey&& aOther)
: PLDHashEntryHdr(std::move(aOther)),
mSupports(std::move(aOther.mSupports)) {}
nsISupportsHashKey(nsISupportsHashKey&& aOther) = default;
~nsISupportsHashKey() = default;
KeyType GetKey() const { return mSupports; }
@ -329,7 +325,7 @@ class nsISupportsHashKey : public PLDHashEntryHdr {
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) {
return NS_PTR_TO_UINT32(aKey) >> 2;
return mozilla::HashGeneric(aKey);
}
enum { ALLOW_MEMMOVE = true };
@ -345,12 +341,11 @@ class nsISupportsHashKey : public PLDHashEntryHdr {
template <class T>
class nsRefPtrHashKey : public PLDHashEntryHdr {
public:
typedef T* KeyType;
typedef const T* KeyTypePointer;
using KeyType = T*;
using KeyTypePointer = const T*;
explicit nsRefPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
nsRefPtrHashKey(nsRefPtrHashKey&& aOther)
: PLDHashEntryHdr(std::move(aOther)), mKey(std::move(aOther.mKey)) {}
nsRefPtrHashKey(nsRefPtrHashKey&& aOther) = default;
~nsRefPtrHashKey() = default;
KeyType GetKey() const { return mKey; }
@ -358,7 +353,7 @@ class nsRefPtrHashKey : public PLDHashEntryHdr {
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) {
return NS_PTR_TO_UINT32(aKey) >> 2;
return mozilla::HashGeneric(aKey);
}
enum { ALLOW_MEMMOVE = true };
@ -373,25 +368,6 @@ inline void ImplCycleCollectionTraverse(
CycleCollectionNoteChild(aCallback, aField.GetKey(), aName, aFlags);
}
/**
* hashkey wrapper using T* KeyType that sets key to nullptr upon
* destruction. Relevant only in cases where a memory pointer-scanner
* like valgrind might get confused about stale references.
*
* @see nsTHashtable::EntryType for specification
*/
template <class T>
class nsClearingPtrHashKey : public nsPtrHashKey<T> {
public:
explicit nsClearingPtrHashKey(const T* aKey) : nsPtrHashKey<T>(aKey) {}
nsClearingPtrHashKey(nsClearingPtrHashKey&& aToMove)
: nsPtrHashKey<T>(std::move(aToMove)) {}
~nsClearingPtrHashKey() { nsPtrHashKey<T>::mKey = nullptr; }
};
typedef nsClearingPtrHashKey<const void> nsClearingVoidPtrHashKey;
/**
* hashkey wrapper using a function pointer KeyType
*
@ -412,7 +388,7 @@ class nsFuncPtrHashKey : public PLDHashEntryHdr {
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) {
return NS_PTR_TO_UINT32(*aKey) >> 2;
return mozilla::HashGeneric(*aKey);
}
enum { ALLOW_MEMMOVE = true };

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

@ -24,12 +24,11 @@
template <class T>
class nsPtrHashKey : public PLDHashEntryHdr {
public:
typedef T* KeyType;
typedef const T* KeyTypePointer;
using KeyType = T*;
using KeyTypePointer = const T*;
explicit nsPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
nsPtrHashKey(nsPtrHashKey<T>&& aToMove)
: PLDHashEntryHdr(std::move(aToMove)), mKey(std::move(aToMove.mKey)) {}
nsPtrHashKey(nsPtrHashKey&& aToMove) = default;
~nsPtrHashKey() = default;
KeyType GetKey() const { return mKey; }
@ -45,6 +44,6 @@ class nsPtrHashKey : public PLDHashEntryHdr {
T* MOZ_NON_OWNING_REF mKey;
};
typedef nsPtrHashKey<const void> nsVoidPtrHashKey;
using nsVoidPtrHashKey = nsPtrHashKey<const void>;
#endif // nsPointerHashKeys_h

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

@ -35,6 +35,11 @@ struct nsKeyClass<nsAtom*> {
using type = nsWeakAtomHashKey;
};
template <typename Ret, typename... Args>
struct nsKeyClass<Ret (*)(Args...)> {
using type = nsFuncPtrHashKey<Ret (*)(Args...)>;
};
template <>
struct nsKeyClass<nsCString> {
using type = nsCStringHashKey;