Skip to content

Commit

Permalink
Bug 1634281 - Merge nsRefPtrHashtable and nsInterfaceHashtable into n…
Browse files Browse the repository at this point in the history
…sRefCountedHashtable. 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
  • Loading branch information
sigiesec committed Mar 4, 2021
1 parent 3004f1f commit c57f081
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 363 deletions.
1 change: 1 addition & 0 deletions xpcom/ds/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ EXPORTS += [
"nsPointerHashKeys.h",
"nsProperties.h",
"nsQuickSort.h",
"nsRefCountedHashtable.h",
"nsRefPtrHashtable.h",
"nsSimpleEnumerator.h",
"nsStaticAtomUtils.h",
Expand Down
32 changes: 29 additions & 3 deletions xpcom/ds/nsHashtablesFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,10 +41,30 @@ class nsClassHashtable;
template <class KeyClass, class DataType>
using nsDataHashtable = nsBaseHashtable<KeyClass, DataType, DataType>;

template <class KeyClass, class PtrType>
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>
class nsInterfaceHashtable;
using nsInterfaceHashtable =
nsRefCountedHashtable<KeyClass, nsCOMPtr<Interface>>;

template <class KeyClass, class PtrType>
class nsRefPtrHashtable;
/**
* 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_
177 changes: 1 addition & 176 deletions xpcom/ds/nsInterfaceHashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Loading

0 comments on commit c57f081

Please sign in to comment.