2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
#ifndef nsTHashKeys_h__
|
|
|
|
#define nsTHashKeys_h__
|
|
|
|
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2013-07-12 04:16:41 +04:00
|
|
|
#include <new>
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2006-06-15 17:48:29 +04:00
|
|
|
#include "nsCRTGlue.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2016-08-23 01:45:47 +03:00
|
|
|
#include "nsPointerHashKeys.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2018-03-21 03:52:16 +03:00
|
|
|
#include <stdint.h>
|
2005-08-15 22:29:55 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-08-25 19:31:08 +03:00
|
|
|
#include <type_traits>
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// These are defined analogously to the HashString overloads in mfbt.
|
|
|
|
|
|
|
|
inline uint32_t HashString(const nsAString& aStr) {
|
|
|
|
return HashString(aStr.BeginReading(), aStr.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t HashString(const nsACString& aStr) {
|
|
|
|
return HashString(aStr.BeginReading(), aStr.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/** @file nsHashKeys.h
|
2005-08-11 23:43:06 +04:00
|
|
|
* standard HashKey classes for nsBaseHashtable and relatives. Each of these
|
2005-08-11 23:42:59 +04:00
|
|
|
* classes follows the nsTHashtable::EntryType specification
|
|
|
|
*
|
2005-08-11 23:43:06 +04:00
|
|
|
* Lightweight keytypes provided here:
|
2005-08-11 23:42:59 +04:00
|
|
|
* nsStringHashKey
|
|
|
|
* nsCStringHashKey
|
|
|
|
* nsUint32HashKey
|
2010-10-21 04:24:16 +04:00
|
|
|
* nsUint64HashKey
|
2012-09-06 08:58:46 +04:00
|
|
|
* nsFloatHashKey
|
2018-03-21 03:52:16 +03:00
|
|
|
* IntPtrHashKey
|
2015-01-29 22:41:57 +03:00
|
|
|
* nsPtrHashKey
|
2009-01-09 03:12:33 +03:00
|
|
|
* nsClearingPtrHashKey
|
2007-02-09 23:44:10 +03:00
|
|
|
* nsVoidPtrHashKey
|
|
|
|
* nsClearingVoidPtrHashKey
|
2005-08-11 23:42:59 +04:00
|
|
|
* nsISupportsHashKey
|
|
|
|
* nsIDHashKey
|
2005-08-11 23:43:11 +04:00
|
|
|
* nsDepCharHashKey
|
2007-02-09 23:44:10 +03:00
|
|
|
* nsCharPtrHashKey
|
|
|
|
* nsUnicharPtrHashKey
|
2013-12-04 04:23:32 +04:00
|
|
|
* nsGenericHashKey
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsAString KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsStringHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
|
|
|
typedef const nsAString& KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsStringHashKey(const nsStringHashKey&) = delete;
|
|
|
|
nsStringHashKey(nsStringHashKey&& aToMove)
|
|
|
|
: PLDHashEntryHdr(std::move(aToMove)), mStr(std::move(aToMove.mStr)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsStringHashKey() = default;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mStr; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(const KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(const KeyTypePointer aKey) {
|
2012-03-13 02:53:18 +04:00
|
|
|
return mozilla::HashString(*aKey);
|
|
|
|
}
|
2014-08-06 00:27:41 +04:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsString mStr;
|
2012-03-13 02:53:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsAString KeyType
|
|
|
|
*
|
|
|
|
* This is internal-API only because nsCaseInsensitiveStringComparator is
|
|
|
|
* internal-only.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsStringCaseInsensitiveHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef const nsAString& KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsStringCaseInsensitiveHashKey(KeyTypePointer aStr) : mStr(*aStr) {
|
2014-06-27 05:35:39 +04:00
|
|
|
// take it easy just deal HashKey
|
|
|
|
}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
|
|
|
|
nsStringCaseInsensitiveHashKey(const nsStringCaseInsensitiveHashKey&) =
|
|
|
|
delete;
|
|
|
|
nsStringCaseInsensitiveHashKey(nsStringCaseInsensitiveHashKey&& aToMove)
|
|
|
|
: PLDHashEntryHdr(std::move(aToMove)), mStr(std::move(aToMove.mStr)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsStringCaseInsensitiveHashKey() = default;
|
2012-03-13 02:53:18 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mStr; }
|
|
|
|
bool KeyEquals(const KeyTypePointer aKey) const {
|
2020-05-27 21:11:12 +03:00
|
|
|
return mStr.Equals(*aKey, nsCaseInsensitiveStringComparator);
|
2012-03-13 02:53:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(const KeyTypePointer aKey) {
|
2014-06-27 05:35:39 +04:00
|
|
|
nsAutoString tmKey(*aKey);
|
|
|
|
ToLowerCase(tmKey);
|
|
|
|
return mozilla::HashString(tmKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
private:
|
|
|
|
const nsString mStr;
|
|
|
|
};
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
#endif
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsACString KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsCStringHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
|
|
|
typedef const nsACString& KeyType;
|
|
|
|
typedef const nsACString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsCStringHashKey(nsCStringHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mStr(std::move(aOther.mStr)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsCStringHashKey() = default;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
KeyType GetKey() const { return mStr; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
2012-03-13 02:53:18 +04:00
|
|
|
return mozilla::HashString(*aKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2014-08-06 00:27:41 +04:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const nsCString mStr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-08-25 19:31:08 +03:00
|
|
|
* hashkey wrapper using integral or enum KeyTypes
|
2005-08-11 23:42:59 +04:00
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2021-08-25 19:31:08 +03:00
|
|
|
template <typename T,
|
|
|
|
std::enable_if_t<std::is_integral_v<T> || std::is_enum_v<T>, int> = 0>
|
|
|
|
class nsIntegralHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
2021-08-25 19:31:08 +03:00
|
|
|
using KeyType = const T&;
|
|
|
|
using KeyTypePointer = const T*;
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2021-08-25 19:31:08 +03:00
|
|
|
explicit nsIntegralHashKey(KeyTypePointer aKey) : mValue(*aKey) {}
|
|
|
|
nsIntegralHashKey(nsIntegralHashKey&& aOther) noexcept
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mValue(aOther.mValue) {}
|
|
|
|
~nsIntegralHashKey() = default;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mValue; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2021-08-25 19:31:08 +03:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return mozilla::HashGeneric(*aKey);
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
2021-08-25 19:31:08 +03:00
|
|
|
const T mValue;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2010-10-21 04:24:16 +04:00
|
|
|
/**
|
2021-08-25 19:31:08 +03:00
|
|
|
* hashkey wrapper using uint32_t KeyType
|
2010-10-21 04:24:16 +04:00
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2021-08-25 19:31:08 +03:00
|
|
|
using nsUint32HashKey = nsIntegralHashKey<uint32_t>;
|
2010-10-21 04:24:16 +04:00
|
|
|
|
2021-08-25 19:31:08 +03:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using uint64_t KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
using nsUint64HashKey = nsIntegralHashKey<uint64_t>;
|
2010-10-21 04:24:16 +04:00
|
|
|
|
2012-09-06 08:58:46 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using float KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsFloatHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef const float& KeyType;
|
|
|
|
typedef const float* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsFloatHashKey(KeyTypePointer aKey) : mValue(*aKey) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsFloatHashKey(nsFloatHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mValue(std::move(aOther.mValue)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsFloatHashKey() = default;
|
2012-09-06 08:58:46 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mValue; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return *reinterpret_cast<const uint32_t*>(aKey);
|
|
|
|
}
|
2012-09-06 08:58:46 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
const float mValue;
|
|
|
|
};
|
|
|
|
|
2018-03-21 03:52:16 +03:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using intptr_t KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2021-08-25 19:31:08 +03:00
|
|
|
using IntPtrHashKey = nsIntegralHashKey<intptr_t>;
|
2018-03-21 03:52:16 +03:00
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsISupports* KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsISupportsHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
|
|
|
typedef nsISupports* KeyType;
|
|
|
|
typedef const nsISupports* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsISupportsHashKey(const nsISupports* aKey)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mSupports(const_cast<nsISupports*>(aKey)) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsISupportsHashKey(nsISupportsHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)),
|
|
|
|
mSupports(std::move(aOther.mSupports)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsISupportsHashKey() = default;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mSupports; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(aKey) >> 2;
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
2005-08-11 23:43:06 +04:00
|
|
|
nsCOMPtr<nsISupports> mSupports;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2011-12-01 01:30:55 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using refcounted * KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class nsRefPtrHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef T* KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsRefPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsRefPtrHashKey(nsRefPtrHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(std::move(aOther.mKey)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsRefPtrHashKey() = default;
|
2011-12-01 01:30:55 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(aKey) >> 2;
|
2011-12-01 01:30:55 +04:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T> mKey;
|
2011-12-01 01:30:55 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class T>
|
2013-09-24 05:47:30 +04:00
|
|
|
inline void ImplCycleCollectionTraverse(
|
|
|
|
nsCycleCollectionTraversalCallback& aCallback, nsRefPtrHashKey<T>& aField,
|
|
|
|
const char* aName, uint32_t aFlags = 0) {
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.GetKey(), aName, aFlags);
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
/**
|
2013-10-11 00:41:39 +04:00
|
|
|
* hashkey wrapper using T* KeyType that sets key to nullptr upon
|
2007-01-05 01:31:26 +03:00
|
|
|
* destruction. Relevant only in cases where a memory pointer-scanner
|
|
|
|
* like valgrind might get confused about stale references.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
|
2009-01-09 03:12:33 +03:00
|
|
|
template <class T>
|
|
|
|
class nsClearingPtrHashKey : public nsPtrHashKey<T> {
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsClearingPtrHashKey(const T* aKey) : nsPtrHashKey<T>(aKey) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsClearingPtrHashKey(nsClearingPtrHashKey&& aToMove)
|
|
|
|
: nsPtrHashKey<T>(std::move(aToMove)) {}
|
2012-07-30 18:20:58 +04:00
|
|
|
~nsClearingPtrHashKey() { nsPtrHashKey<T>::mKey = nullptr; }
|
2005-08-11 23:43:14 +04:00
|
|
|
};
|
|
|
|
|
2009-01-09 03:12:33 +03:00
|
|
|
typedef nsClearingPtrHashKey<const void> nsClearingVoidPtrHashKey;
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using a function pointer KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class nsFuncPtrHashKey : public PLDHashEntryHdr {
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
|
|
|
typedef T& KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsFuncPtrHashKey(const T* aKey) : mKey(*const_cast<T*>(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsFuncPtrHashKey(const nsFuncPtrHashKey<T>& aToCopy) : mKey(aToCopy.mKey) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsFuncPtrHashKey() = default;
|
2013-07-09 18:28:15 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return const_cast<T&>(mKey); }
|
2014-06-27 05:35:39 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(*aKey) >> 2;
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
protected:
|
2013-07-09 18:28:15 +04:00
|
|
|
T mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsID KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsIDHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
|
|
|
typedef const nsID& KeyType;
|
|
|
|
typedef const nsID* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsIDHashKey(const nsID* aInID) : mID(*aInID) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsIDHashKey(nsIDHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mID(std::move(aOther.mID)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsIDHashKey() = default;
|
2005-08-11 23:43:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
KeyType GetKey() const { return mID; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2012-03-13 02:53:18 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
// Hash the nsID object's raw bytes.
|
|
|
|
return mozilla::HashBytes(aKey, sizeof(KeyType));
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsID mID;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2018-07-22 05:09:25 +03:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsID* KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsIDPointerHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef const nsID* KeyType;
|
|
|
|
typedef const nsID* KeyTypePointer;
|
|
|
|
|
|
|
|
explicit nsIDPointerHashKey(const nsID* aInID) : mID(aInID) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsIDPointerHashKey(nsIDPointerHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mID(aOther.mID) {}
|
2018-07-22 05:09:25 +03:00
|
|
|
~nsIDPointerHashKey() = default;
|
|
|
|
|
|
|
|
KeyType GetKey() const { return mID; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(*mID); }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
// Hash the nsID object's raw bytes.
|
|
|
|
return mozilla::HashBytes(aKey, sizeof(*aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsID* mID;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:43:11 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper for "dependent" const char*; this class does not "own"
|
|
|
|
* its string pointer.
|
|
|
|
*
|
|
|
|
* This class must only be used if the strings have a lifetime longer than
|
|
|
|
* the hashtable they occupy. This normally occurs only for static
|
|
|
|
* strings or strings that have been arena-allocated.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsDepCharHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:43:11 +04:00
|
|
|
public:
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsDepCharHashKey(const char* aKey) : mKey(aKey) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsDepCharHashKey(nsDepCharHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(std::move(aOther.mKey)) {}
|
2020-02-12 14:13:33 +03:00
|
|
|
~nsDepCharHashKey() = default;
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
const char* GetKey() const { return mKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
bool KeyEquals(const char* aKey) const { return !strcmp(mKey, aKey); }
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
static const char* KeyToPointer(const char* aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(const char* aKey) {
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:43:13 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper for const char*; at construction, this class duplicates
|
|
|
|
* a string pointed to by the pointer so that it doesn't matter whether or not
|
|
|
|
* the string lives longer than the hash table.
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsCharPtrHashKey : public PLDHashEntryHdr {
|
2005-08-11 23:43:13 +04:00
|
|
|
public:
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) {}
|
2013-08-20 20:08:00 +04:00
|
|
|
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsCharPtrHashKey(const nsCharPtrHashKey&) = delete;
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCharPtrHashKey(nsCharPtrHashKey&& aOther)
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(aOther.mKey) {
|
2014-06-27 05:35:39 +04:00
|
|
|
aOther.mKey = nullptr;
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~nsCharPtrHashKey() {
|
|
|
|
if (mKey) {
|
|
|
|
free(const_cast<char*>(mKey));
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const char* GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return !strcmp(mKey, aKey); }
|
|
|
|
|
2005-08-11 23:43:13 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
2014-07-31 00:00:29 +04:00
|
|
|
return aMallocSizeOf(mKey);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:43:13 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* mKey;
|
|
|
|
};
|
|
|
|
|
2006-06-15 17:48:29 +04:00
|
|
|
/**
|
2014-01-04 19:02:17 +04:00
|
|
|
* hashkey wrapper for const char16_t*; at construction, this class duplicates
|
2006-06-15 17:48:29 +04:00
|
|
|
* a string pointed to by the pointer so that it doesn't matter whether or not
|
|
|
|
* the string lives longer than the hash table.
|
|
|
|
*/
|
|
|
|
class nsUnicharPtrHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
2014-01-04 19:02:17 +04:00
|
|
|
typedef const char16_t* KeyType;
|
|
|
|
typedef const char16_t* KeyTypePointer;
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2018-08-28 08:58:54 +03:00
|
|
|
explicit nsUnicharPtrHashKey(const char16_t* aKey) : mKey(NS_xstrdup(aKey)) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsUnicharPtrHashKey(const nsUnicharPtrHashKey& aToCopy) = delete;
|
2014-06-27 05:35:39 +04:00
|
|
|
nsUnicharPtrHashKey(nsUnicharPtrHashKey&& aOther)
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(aOther.mKey) {
|
2014-06-27 05:35:39 +04:00
|
|
|
aOther.mKey = nullptr;
|
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~nsUnicharPtrHashKey() {
|
|
|
|
if (mKey) {
|
2017-10-12 05:08:44 +03:00
|
|
|
free(const_cast<char16_t*>(mKey));
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const char16_t* GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return !NS_strcmp(mKey, aKey); }
|
|
|
|
|
2006-06-15 17:48:29 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(mKey);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2006-06-15 17:48:29 +04:00
|
|
|
|
|
|
|
private:
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* mKey;
|
2006-06-15 17:48:29 +04:00
|
|
|
};
|
|
|
|
|
2015-08-27 04:19:31 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
PLDHashNumber Hash(const T& aValue) {
|
|
|
|
return aValue.Hash();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-12-04 04:23:32 +04:00
|
|
|
/**
|
|
|
|
* Hashtable key class to use with objects for which Hash() and operator==()
|
|
|
|
* are defined.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template <typename T>
|
2013-12-04 04:23:32 +04:00
|
|
|
class nsGenericHashKey : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef const T& KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsGenericHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
|
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.
2018-09-20 18:20:36 +03:00
|
|
|
nsGenericHashKey(const nsGenericHashKey&) = delete;
|
|
|
|
nsGenericHashKey(nsGenericHashKey&& aOther)
|
|
|
|
: PLDHashEntryHdr(std::move(aOther)), mKey(std::move(aOther.mKey)) {}
|
2013-12-04 04:23:32 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2015-08-27 04:19:31 +03:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) {
|
|
|
|
return ::mozilla::Hash(*aKey);
|
|
|
|
}
|
2013-12-04 04:23:32 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
T mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsTHashKeys_h__
|