Skip to content

Commit e7b3b31

Browse files
committed
Bug 1415980 - make hash keys movable and not copyable; r=erahm
Everything that goes in a PLDHashtable (and its derivatives, like nsTHashtable) needs to inherit from PLDHashEntryHdr. But through a lack of enforcement, copy constructors for these derived classes didn't explicitly invoke the copy constructor for PLDHashEntryHdr (and the compiler didn't invoke the copy constructor for us). Instead, PLDHashTable explicitly copied around the bits that the copy constructor would have. The current setup has two problems: 1) Derived classes should be using move construction, not copy construction, since anything that's shuffling hash table keys/entries around will be using move construction. 2) Derived classes should take responsibility for transferring bits of superclass state around, and not rely on something else to handle that. The second point is not a huge problem for PLDHashTable (PLDHashTable only has to copy PLDHashEntryHdr's bits in a single place), but future hash table implementations that might move entries around more aggressively would have to insert compensation code all over the place. Additionally, if moving entries is implemented via memcpy (which is quite common), PLDHashTable copying around bits *again* is inefficient. Let's fix all these problems in one go, by: 1) Explicitly declaring the set of constructors that PLDHashEntryHdr implements (and does not implement). In particular, the copy constructor is deleted, so any derived classes that attempt to make themselves copyable will be detected at compile time: the compiler will complain that the superclass type is not copyable. This change on its own will result in many compiler errors, so... 2) Change any derived classes to implement move constructors instead of copy constructors. Note that some of these move constructors are, strictly speaking, unnecessary, since the relevant classes are moved via memcpy in nsTHashtable and its derivatives.
1 parent 31e8ed3 commit e7b3b31

34 files changed

+175
-98
lines changed

accessible/base/NotificationController.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,10 @@ class NotificationController final : public EventQueue,
367367
typedef const T* KeyTypePointer;
368368

369369
explicit nsCOMPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
370-
explicit nsCOMPtrHashKey(const nsPtrHashKey<T> &aToCopy) : mKey(aToCopy.mKey) {}
370+
nsCOMPtrHashKey(nsCOMPtrHashKey<T>&& aOther)
371+
: PLDHashEntryHdr(std::move(aOther))
372+
, mKey(std::move(aOther.mKey))
373+
{}
371374
~nsCOMPtrHashKey() { }
372375

373376
KeyType GetKey() const { return mKey; }

dom/animation/PseudoElementHashEntry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PseudoElementHashEntry : public PLDHashEntryHdr
2424
explicit PseudoElementHashEntry(KeyTypePointer aKey)
2525
: mElement(aKey->mElement)
2626
, mPseudoType(aKey->mPseudoType) { }
27-
explicit PseudoElementHashEntry(const PseudoElementHashEntry& aCopy)=default;
27+
PseudoElementHashEntry(PseudoElementHashEntry&& aOther) = default;
2828

2929
~PseudoElementHashEntry() = default;
3030

dom/base/nsDocument.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ nsIdentifierMapEntry::~nsIdentifierMapEntry()
392392
{}
393393

394394
nsIdentifierMapEntry::nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther)
395-
: mKey(std::move(aOther.mKey))
395+
: PLDHashEntryHdr(std::move(aOther))
396+
, mKey(std::move(aOther.mKey))
396397
, mIdContentList(std::move(aOther.mIdContentList))
397398
, mNameContentList(std::move(aOther.mNameContentList))
398399
, mChangeCallbacks(std::move(aOther.mChangeCallbacks))

dom/base/nsIdentifierMapEntry.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ class nsIdentifierMapEntry : public PLDHashEntryHdr
182182

183183
explicit ChangeCallbackEntry(const ChangeCallback* aKey) :
184184
mKey(*aKey) { }
185-
ChangeCallbackEntry(const ChangeCallbackEntry& toCopy) :
186-
mKey(toCopy.mKey) { }
185+
ChangeCallbackEntry(ChangeCallbackEntry&& aOther) :
186+
PLDHashEntryHdr(std::move(aOther)),
187+
mKey(std::move(aOther.mKey)) { }
187188

188189
KeyType GetKey() const { return mKey; }
189190
bool KeyEquals(KeyTypePointer aKey) const {

dom/base/nsNodeInfoManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class nsNodeInfoManager final
148148
{
149149
public:
150150
explicit NodeInfoInnerKey(KeyTypePointer aKey) : nsPtrHashKey(aKey) {}
151+
NodeInfoInnerKey(NodeInfoInnerKey&&) = default;
151152
~NodeInfoInnerKey() = default;
152153
bool KeyEquals(KeyTypePointer aKey) const { return *mKey == *aKey; }
153154
static PLDHashNumber HashKey(KeyTypePointer aKey) { return aKey->Hash(); }

dom/commandhandler/nsCommandParams.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ nsCommandParams::HashMoveEntry(PLDHashTable* aTable,
336336
const PLDHashEntryHdr* aFrom,
337337
PLDHashEntryHdr* aTo)
338338
{
339-
const HashEntry* fromEntry = static_cast<const HashEntry*>(aFrom);
339+
auto* fromEntry = const_cast<HashEntry*>(static_cast<const HashEntry*>(aFrom));
340340
HashEntry* toEntry = static_cast<HashEntry*>(aTo);
341341

342-
new (toEntry) HashEntry(*fromEntry);
342+
new (KnownNotNull, toEntry) HashEntry(std::move(*fromEntry));
343343

344344
fromEntry->~HashEntry();
345345
}

dom/commandhandler/nsCommandParams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class nsCommandParams : public nsICommandParams
8686
Reset(mEntryType);
8787
}
8888

89-
HashEntry(const HashEntry& aRHS)
89+
explicit HashEntry(const HashEntry& aRHS)
9090
: mEntryType(aRHS.mEntryType)
9191
{
9292
Reset(mEntryType);

dom/html/HTMLMediaElement.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,15 +3689,10 @@ HTMLMediaElement::MozCaptureStreamUntilEnded(ErrorResult& aRv)
36893689
class MediaElementSetForURI : public nsURIHashKey
36903690
{
36913691
public:
3692-
explicit MediaElementSetForURI(const nsIURI* aKey)
3693-
: nsURIHashKey(aKey)
3694-
{
3695-
}
3696-
MediaElementSetForURI(const MediaElementSetForURI& toCopy)
3697-
: nsURIHashKey(toCopy)
3698-
, mElements(toCopy.mElements)
3699-
{
3700-
}
3692+
explicit MediaElementSetForURI(const nsIURI* aKey) : nsURIHashKey(aKey) {}
3693+
MediaElementSetForURI(MediaElementSetForURI&& aOther)
3694+
: nsURIHashKey(std::move(aOther))
3695+
, mElements(std::move(aOther.mElements)) {}
37013696
nsTArray<HTMLMediaElement*> mElements;
37023697
};
37033698

dom/smil/nsSMILCompositor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class nsSMILCompositor : public PLDHashEntryHdr
3535
mForceCompositing(false)
3636
{ }
3737
nsSMILCompositor(nsSMILCompositor&& toMove)
38-
: mKey(std::move(toMove.mKey)),
38+
: PLDHashEntryHdr(std::move(toMove)),
39+
mKey(std::move(toMove.mKey)),
3940
mAnimationFunctions(std::move(toMove.mAnimationFunctions)),
4041
mForceCompositing(false)
4142
{ }

dom/storage/LocalStorageManager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ class LocalStorageManager final : public nsIDOMStorageManager
6666
, mCache(new LocalStorageCache(aKey))
6767
{}
6868

69-
LocalStorageCacheHashKey(const LocalStorageCacheHashKey& aOther)
70-
: nsCStringHashKey(aOther)
69+
LocalStorageCacheHashKey(LocalStorageCacheHashKey&& aOther)
70+
: nsCStringHashKey(std::move(aOther))
71+
, mCache(std::move(aOther.mCache))
72+
, mCacheRef(std::move(aOther.mCacheRef))
7173
{
7274
NS_ERROR("Shouldn't be called");
7375
}

0 commit comments

Comments
 (0)