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
|
|
|
|
2018-07-26 13:15:55 +03:00
|
|
|
// See the comment at the top of mfbt/HashTable.h for a comparison between
|
|
|
|
// PLDHashTable and mozilla::HashTable.
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#ifndef nsTHashtable_h__
|
|
|
|
#define nsTHashtable_h__
|
|
|
|
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2016-08-23 01:45:47 +03:00
|
|
|
#include "nsPointerHashKeys.h"
|
2015-01-13 17:44:33 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-10-03 03:32:15 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/fallible.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-08-20 20:08:00 +04:00
|
|
|
#include "mozilla/Move.h"
|
2016-08-23 01:40:10 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2013-09-02 12:41:57 +04:00
|
|
|
#include "mozilla/PodOperations.h"
|
2015-10-03 03:32:15 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2013-08-29 22:54:14 +04:00
|
|
|
#include <new>
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* a base class for templated hashtables.
|
|
|
|
*
|
|
|
|
* Clients will rarely need to use this class directly. Check the derived
|
|
|
|
* classes first, to see if they will meet your needs.
|
|
|
|
*
|
|
|
|
* @param EntryType the templated entry-type class that is managed by the
|
|
|
|
* hashtable. <code>EntryType</code> must extend the following declaration,
|
|
|
|
* and <strong>must not declare any virtual functions or derive from classes
|
|
|
|
* with virtual functions.</strong> Any vtable pointer would break the
|
|
|
|
* PLDHashTable code.
|
2005-08-11 23:43:06 +04:00
|
|
|
*<pre> class EntryType : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
* {
|
|
|
|
* public: or friend nsTHashtable<EntryType>;
|
|
|
|
* // KeyType is what we use when Get()ing or Put()ing this entry
|
2012-08-22 19:56:38 +04:00
|
|
|
* // this should either be a simple datatype (uint32_t, nsISupports*) or
|
2005-08-11 23:42:59 +04:00
|
|
|
* // a const reference (const nsAString&)
|
|
|
|
* typedef something KeyType;
|
2015-09-16 06:49:53 +03:00
|
|
|
* // KeyTypePointer is the pointer-version of KeyType, because
|
|
|
|
* // PLDHashTable.h requires keys to cast to <code>const void*</code>
|
2005-08-11 23:42:59 +04:00
|
|
|
* typedef const something* KeyTypePointer;
|
|
|
|
*
|
|
|
|
* EntryType(KeyTypePointer aKey);
|
|
|
|
*
|
2013-08-29 22:54:14 +04:00
|
|
|
* // A copy or C++11 Move constructor must be defined, even if
|
|
|
|
* // AllowMemMove() == true, otherwise you will cause link errors.
|
|
|
|
* EntryType(const EntryType& aEnt); // Either this...
|
|
|
|
* EntryType(EntryType&& aEnt); // ...or this
|
2005-08-11 23:42:59 +04:00
|
|
|
*
|
|
|
|
* // the destructor must be defined... or you will cause link errors!
|
|
|
|
* ~EntryType();
|
|
|
|
*
|
|
|
|
* // KeyEquals(): does this entry match this key?
|
2011-09-29 10:19:26 +04:00
|
|
|
* bool KeyEquals(KeyTypePointer aKey) const;
|
2005-08-11 23:42:59 +04:00
|
|
|
*
|
|
|
|
* // KeyToPointer(): Convert KeyType to KeyTypePointer
|
|
|
|
* static KeyTypePointer KeyToPointer(KeyType aKey);
|
|
|
|
*
|
|
|
|
* // HashKey(): calculate the hash number
|
|
|
|
* static PLDHashNumber HashKey(KeyTypePointer aKey);
|
2007-08-09 01:41:43 +04:00
|
|
|
*
|
|
|
|
* // ALLOW_MEMMOVE can we move this class with memmove(), or do we have
|
|
|
|
* // to use the copy constructor?
|
2013-08-16 03:33:54 +04:00
|
|
|
* enum { ALLOW_MEMMOVE = true/false };
|
2005-08-11 23:42:59 +04:00
|
|
|
* }</pre>
|
|
|
|
*
|
|
|
|
* @see nsInterfaceHashtable
|
|
|
|
* @see nsDataHashtable
|
|
|
|
* @see nsClassHashtable
|
|
|
|
* @author "Benjamin Smedberg <bsmedberg@covad.net>"
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class EntryType>
|
2015-06-20 02:37:43 +03:00
|
|
|
class MOZ_NEEDS_NO_VTABLE_TYPE nsTHashtable {
|
2012-05-18 20:42:01 +04:00
|
|
|
typedef mozilla::fallible_t fallible_t;
|
2015-10-03 03:32:15 +03:00
|
|
|
static_assert(mozilla::IsPointer<typename EntryType::KeyTypePointer>::value,
|
|
|
|
"KeyTypePointer should be a pointer");
|
2012-05-18 20:42:01 +04:00
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
public:
|
2014-08-06 17:31:21 +04:00
|
|
|
// Separate constructors instead of default aInitLength parameter since
|
2013-09-02 12:41:57 +04:00
|
|
|
// otherwise the default no-arg constructor isn't found.
|
2015-05-13 03:33:49 +03:00
|
|
|
nsTHashtable()
|
2015-07-21 03:06:38 +03:00
|
|
|
: mTable(Ops(), sizeof(EntryType), PLDHashTable::kDefaultInitialLength) {}
|
2015-05-13 03:33:49 +03:00
|
|
|
explicit nsTHashtable(uint32_t aInitLength)
|
|
|
|
: mTable(Ops(), sizeof(EntryType), aInitLength) {}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* destructor, cleans up and deallocates
|
|
|
|
*/
|
|
|
|
~nsTHashtable();
|
|
|
|
|
2013-08-29 22:54:14 +04:00
|
|
|
nsTHashtable(nsTHashtable<EntryType>&& aOther);
|
2018-09-25 05:23:17 +03:00
|
|
|
nsTHashtable<EntryType>& operator=(nsTHashtable<EntryType>&& aOther);
|
2013-08-20 20:08:00 +04:00
|
|
|
|
2008-06-23 03:12:40 +04:00
|
|
|
/**
|
|
|
|
* Return the generation number for the table. This increments whenever
|
|
|
|
* the table data items are moved.
|
|
|
|
*/
|
2014-08-26 03:56:33 +04:00
|
|
|
uint32_t GetGeneration() const { return mTable.Generation(); }
|
2008-06-23 03:12:40 +04:00
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* KeyType is typedef'ed for ease of use.
|
|
|
|
*/
|
|
|
|
typedef typename EntryType::KeyType KeyType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KeyTypePointer is typedef'ed for ease of use.
|
|
|
|
*/
|
|
|
|
typedef typename EntryType::KeyTypePointer KeyTypePointer;
|
|
|
|
|
2005-08-11 23:43:11 +04:00
|
|
|
/**
|
|
|
|
* Return the number of entries in the table.
|
|
|
|
* @return number of entries
|
|
|
|
*/
|
2014-08-26 03:56:33 +04:00
|
|
|
uint32_t Count() const { return mTable.EntryCount(); }
|
2005-08-11 23:43:11 +04:00
|
|
|
|
2015-10-08 06:31:17 +03:00
|
|
|
/**
|
|
|
|
* Return true if the hashtable is empty.
|
|
|
|
*/
|
|
|
|
bool IsEmpty() const { return Count() == 0; }
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* Get the entry associated with a key.
|
|
|
|
* @param aKey the key to retrieve
|
2012-07-30 18:20:58 +04:00
|
|
|
* @return pointer to the entry class, if the key exists; nullptr if the
|
2005-08-11 23:42:59 +04:00
|
|
|
* key doesn't exist
|
|
|
|
*/
|
2005-08-11 23:43:06 +04:00
|
|
|
EntryType* GetEntry(KeyType aKey) const {
|
2015-01-23 08:06:55 +03:00
|
|
|
return static_cast<EntryType*>(
|
2018-07-13 13:01:53 +03:00
|
|
|
mTable.Search(EntryType::KeyToPointer(aKey)));
|
2005-08-11 23:43:06 +04:00
|
|
|
}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2012-02-22 18:32:13 +04:00
|
|
|
/**
|
|
|
|
* Return true if an entry for the given key exists, false otherwise.
|
|
|
|
* @param aKey the key to retrieve
|
|
|
|
* @return true if the key exists, false if the key doesn't exist
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
bool Contains(KeyType aKey) const { return !!GetEntry(aKey); }
|
2012-02-22 18:32:13 +04:00
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
2017-07-28 13:10:04 +03:00
|
|
|
* Infallibly get the entry associated with a key, or create a new entry,
|
2005-08-11 23:42:59 +04:00
|
|
|
* @param aKey the key to retrieve
|
2017-07-28 13:10:04 +03:00
|
|
|
* @return pointer to the entry retrieved; never nullptr
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
2005-08-11 23:43:06 +04:00
|
|
|
EntryType* PutEntry(KeyType aKey) {
|
2015-09-15 00:23:12 +03:00
|
|
|
// infallible add
|
|
|
|
return static_cast<EntryType*>(mTable.Add(EntryType::KeyToPointer(aKey)));
|
2012-05-18 20:42:01 +04:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:10:04 +03:00
|
|
|
/**
|
|
|
|
* Fallibly get the entry associated with a key, or create a new entry,
|
|
|
|
* @param aKey the key to retrieve
|
|
|
|
* @return pointer to the entry retrieved; nullptr only if memory can't
|
|
|
|
* be allocated
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-03-19 10:46:40 +03:00
|
|
|
EntryType* PutEntry(KeyType aKey, const fallible_t&) {
|
2015-09-15 00:23:12 +03:00
|
|
|
return static_cast<EntryType*>(
|
|
|
|
mTable.Add(EntryType::KeyToPointer(aKey), mozilla::fallible));
|
2005-08-11 23:43:06 +04:00
|
|
|
}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2017-06-17 01:06:04 +03:00
|
|
|
/**
|
|
|
|
* Get the entry associated with a key, or create a new entry using infallible
|
|
|
|
* allocation and insert that.
|
|
|
|
* @param aKey the key to retrieve
|
|
|
|
* @param aEntry will be assigned (if non-null) to the entry that was
|
|
|
|
* found or created
|
|
|
|
* @return true if a new entry was created, or false if an existing entry
|
|
|
|
* was found
|
|
|
|
*/
|
|
|
|
MOZ_MUST_USE
|
|
|
|
bool EnsureInserted(KeyType aKey, EntryType** aEntry = nullptr) {
|
|
|
|
auto oldCount = Count();
|
|
|
|
EntryType* entry = PutEntry(aKey);
|
|
|
|
if (aEntry) {
|
|
|
|
*aEntry = entry;
|
|
|
|
}
|
|
|
|
return oldCount != Count();
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* Remove the entry associated with a key.
|
|
|
|
* @param aKey of the entry to remove
|
|
|
|
*/
|
2005-08-11 23:43:06 +04:00
|
|
|
void RemoveEntry(KeyType aKey) {
|
2015-09-15 00:23:24 +03:00
|
|
|
mTable.Remove(EntryType::KeyToPointer(aKey));
|
2005-08-11 23:43:06 +04:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:06:04 +03:00
|
|
|
/**
|
|
|
|
* Lookup the entry associated with aKey and remove it if found, otherwise
|
|
|
|
* do nothing.
|
|
|
|
* @param aKey of the entry to remove
|
|
|
|
* @return true if an entry was found and removed, or false if no entry
|
|
|
|
* was found for aKey
|
|
|
|
*/
|
|
|
|
bool EnsureRemoved(KeyType aKey) {
|
|
|
|
auto* entry = GetEntry(aKey);
|
|
|
|
if (entry) {
|
|
|
|
RemoveEntry(entry);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-25 02:12:38 +03:00
|
|
|
/**
|
|
|
|
* Remove the entry associated with a key.
|
|
|
|
* @param aEntry the entry-pointer to remove (obtained from GetEntry)
|
|
|
|
*/
|
|
|
|
void RemoveEntry(EntryType* aEntry) { mTable.RemoveEntry(aEntry); }
|
|
|
|
|
2005-08-11 23:43:06 +04:00
|
|
|
/**
|
|
|
|
* Remove the entry associated with a key, but don't resize the hashtable.
|
|
|
|
* This is a low-level method, and is not recommended unless you know what
|
2015-09-25 02:12:38 +03:00
|
|
|
* you're doing. If you use it, please add a comment explaining why you
|
|
|
|
* didn't use RemoveEntry().
|
|
|
|
* @param aEntry the entry-pointer to remove (obtained from GetEntry)
|
2005-08-11 23:43:06 +04:00
|
|
|
*/
|
|
|
|
void RawRemoveEntry(EntryType* aEntry) { mTable.RawRemove(aEntry); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2015-07-08 06:47:59 +03:00
|
|
|
// This is an iterator that also allows entry removal. Example usage:
|
|
|
|
//
|
|
|
|
// for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
// Entry* entry = iter.Get();
|
|
|
|
// // ... do stuff with |entry| ...
|
|
|
|
// // ... possibly call iter.Remove() once ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
class Iterator : public PLDHashTable::Iterator {
|
|
|
|
public:
|
|
|
|
typedef PLDHashTable::Iterator Base;
|
|
|
|
|
|
|
|
explicit Iterator(nsTHashtable* aTable) : Base(&aTable->mTable) {}
|
|
|
|
Iterator(Iterator&& aOther) : Base(aOther.mTable) {}
|
|
|
|
~Iterator() {}
|
|
|
|
|
|
|
|
EntryType* Get() const { return static_cast<EntryType*>(Base::Get()); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Iterator() = delete;
|
|
|
|
Iterator(const Iterator&) = delete;
|
|
|
|
Iterator& operator=(const Iterator&) = delete;
|
|
|
|
Iterator& operator=(const Iterator&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator Iter() { return Iterator(this); }
|
|
|
|
|
|
|
|
Iterator ConstIter() const {
|
|
|
|
return Iterator(const_cast<nsTHashtable*>(this));
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
2015-06-10 23:07:40 +03:00
|
|
|
* Remove all entries, return hashtable to "pristine" state. It's
|
|
|
|
* conceptually the same as calling the destructor and then re-calling the
|
|
|
|
* constructor.
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
2015-06-10 23:07:40 +03:00
|
|
|
void Clear() { mTable.Clear(); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2011-11-28 07:03:14 +04:00
|
|
|
/**
|
2015-07-29 11:50:52 +03:00
|
|
|
* Measure the size of the table's entry storage. Does *not* measure anything
|
|
|
|
* hanging off table entries; hence the "Shallow" prefix. To measure that,
|
|
|
|
* either use SizeOfExcludingThis() or iterate manually over the entries,
|
|
|
|
* calling SizeOfExcludingThis() on each one.
|
2014-06-27 05:35:39 +04:00
|
|
|
*
|
2015-07-29 11:50:52 +03:00
|
|
|
* @param aMallocSizeOf the function used to measure heap-allocated blocks
|
|
|
|
* @return the measured shallow size of the table
|
2011-11-28 07:03:14 +04:00
|
|
|
*/
|
2015-07-29 11:50:52 +03:00
|
|
|
size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return mTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2011-09-15 04:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:00:29 +04:00
|
|
|
/**
|
2015-07-29 11:50:52 +03:00
|
|
|
* Like ShallowSizeOfExcludingThis, but includes sizeof(*this).
|
2014-07-31 00:00:29 +04:00
|
|
|
*/
|
2015-07-29 11:50:52 +03:00
|
|
|
size_t ShallowSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-07-31 00:00:29 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 21:37:54 +04:00
|
|
|
/**
|
2015-07-29 11:50:52 +03:00
|
|
|
* This is a "deep" measurement of the table. To use it, |EntryType| must
|
|
|
|
* define SizeOfExcludingThis, and that method will be called on all live
|
|
|
|
* entries.
|
2014-05-01 21:37:54 +04:00
|
|
|
*/
|
2015-07-29 11:50:52 +03:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
n += (*iter.Get()).SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return n;
|
2014-05-01 21:37:54 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:00:29 +04:00
|
|
|
/**
|
2015-07-29 11:50:52 +03:00
|
|
|
* Like SizeOfExcludingThis, but includes sizeof(*this).
|
2014-07-31 00:00:29 +04:00
|
|
|
*/
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
2015-07-29 11:50:52 +03:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
2014-07-31 00:00:29 +04:00
|
|
|
}
|
|
|
|
|
2015-01-13 17:44:33 +03:00
|
|
|
/**
|
|
|
|
* Swap the elements in this hashtable with the elements in aOther.
|
|
|
|
*/
|
|
|
|
void SwapElements(nsTHashtable<EntryType>& aOther) {
|
2015-01-20 03:34:44 +03:00
|
|
|
MOZ_ASSERT_IF(this->mTable.Ops() && aOther.mTable.Ops(),
|
|
|
|
this->mTable.Ops() == aOther.mTable.Ops());
|
2015-01-13 17:44:33 +03:00
|
|
|
mozilla::Swap(this->mTable, aOther.mTable);
|
|
|
|
}
|
|
|
|
|
2012-01-11 00:31:34 +04:00
|
|
|
/**
|
|
|
|
* Mark the table as constant after initialization.
|
|
|
|
*
|
|
|
|
* This will prevent assertions when a read-only hash is accessed on multiple
|
|
|
|
* threads without synchronization.
|
|
|
|
*/
|
2015-09-15 00:23:27 +03:00
|
|
|
void MarkImmutable() { mTable.MarkImmutable(); }
|
2012-01-11 00:31:34 +04:00
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
protected:
|
2015-05-20 02:46:17 +03:00
|
|
|
PLDHashTable mTable;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2016-03-16 07:33:44 +03:00
|
|
|
static PLDHashNumber s_HashKey(const void* aKey);
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2016-03-16 07:33:44 +03:00
|
|
|
static bool s_MatchEntry(const PLDHashEntryHdr* aEntry, const void* aKey);
|
2007-08-09 01:41:43 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static void s_CopyEntry(PLDHashTable* aTable, const PLDHashEntryHdr* aFrom,
|
|
|
|
PLDHashEntryHdr* aTo);
|
2005-08-11 23:43:02 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static void s_ClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry);
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2015-02-11 20:46:40 +03:00
|
|
|
static void s_InitEntry(PLDHashEntryHdr* aEntry, const void* aKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2005-08-11 23:43:11 +04:00
|
|
|
private:
|
|
|
|
// copy constructor, not implemented
|
2015-01-07 02:35:02 +03:00
|
|
|
nsTHashtable(nsTHashtable<EntryType>& aToCopy) = delete;
|
2013-09-02 12:41:57 +04:00
|
|
|
|
|
|
|
/**
|
2015-05-13 03:33:49 +03:00
|
|
|
* Gets the table's ops.
|
2013-09-02 12:41:57 +04:00
|
|
|
*/
|
2015-05-13 03:33:49 +03:00
|
|
|
static const PLDHashTableOps* Ops();
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
// assignment operator, not implemented
|
2015-01-07 02:35:02 +03:00
|
|
|
nsTHashtable<EntryType>& operator=(nsTHashtable<EntryType>& aToEqual) =
|
|
|
|
delete;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2018-02-06 00:18:12 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Like PLDHashTable::MoveEntryStub, but specialized for fixed N (i.e. the size
|
|
|
|
// of the entries in the hashtable). Saves a memory read to figure out the size
|
|
|
|
// from the table and gives the compiler the opportunity to inline the memcpy.
|
|
|
|
//
|
|
|
|
// We define this outside of nsTHashtable so only one copy exists for every N,
|
|
|
|
// rather than separate copies for every EntryType used with nsTHashtable.
|
|
|
|
template <size_t N>
|
|
|
|
static void FixedSizeEntryMover(PLDHashTable*, const PLDHashEntryHdr* aFrom,
|
|
|
|
PLDHashEntryHdr* aTo) {
|
|
|
|
memcpy(aTo, aFrom, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2005-08-11 23:43:01 +04:00
|
|
|
//
|
|
|
|
// template definitions
|
|
|
|
//
|
|
|
|
|
2013-08-20 20:08:00 +04:00
|
|
|
template <class EntryType>
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTHashtable<EntryType>::nsTHashtable(nsTHashtable<EntryType>&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mTable(std::move(aOther.mTable)) {}
|
2013-08-20 20:08:00 +04:00
|
|
|
|
2018-09-25 05:23:17 +03:00
|
|
|
template <class EntryType>
|
|
|
|
nsTHashtable<EntryType>& nsTHashtable<EntryType>::operator=(
|
|
|
|
nsTHashtable<EntryType>&& aOther) {
|
|
|
|
mTable = std::move(aOther.mTable);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:43:01 +04:00
|
|
|
template <class EntryType>
|
|
|
|
nsTHashtable<EntryType>::~nsTHashtable() {}
|
|
|
|
|
|
|
|
template <class EntryType>
|
2015-05-13 03:33:49 +03:00
|
|
|
/* static */ const PLDHashTableOps* nsTHashtable<EntryType>::Ops() {
|
|
|
|
// If this variable is a global variable, we get strange start-up failures on
|
|
|
|
// WindowsCrtPatch.h (see bug 1166598 comment 20). But putting it inside a
|
|
|
|
// function avoids that problem.
|
2013-11-19 06:34:53 +04:00
|
|
|
static const PLDHashTableOps sOps = {
|
2007-08-09 01:41:43 +04:00
|
|
|
s_HashKey, s_MatchEntry,
|
2018-02-06 00:18:12 +03:00
|
|
|
EntryType::ALLOW_MEMMOVE
|
|
|
|
? mozilla::detail::FixedSizeEntryMover<sizeof(EntryType)>
|
|
|
|
: s_CopyEntry,
|
2007-08-09 01:41:43 +04:00
|
|
|
s_ClearEntry, s_InitEntry};
|
2015-05-13 03:33:49 +03:00
|
|
|
return &sOps;
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static definitions
|
|
|
|
|
|
|
|
template <class EntryType>
|
2016-03-16 07:33:44 +03:00
|
|
|
PLDHashNumber nsTHashtable<EntryType>::s_HashKey(const void* aKey) {
|
2017-10-23 23:42:35 +03:00
|
|
|
return EntryType::HashKey(static_cast<KeyTypePointer>(aKey));
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class EntryType>
|
2016-03-16 07:33:44 +03:00
|
|
|
bool nsTHashtable<EntryType>::s_MatchEntry(const PLDHashEntryHdr* aEntry,
|
2014-06-27 05:35:39 +04:00
|
|
|
const void* aKey) {
|
2018-11-26 20:39:11 +03:00
|
|
|
return (static_cast<const EntryType*>(aEntry))
|
2017-10-23 23:42:35 +03:00
|
|
|
->KeyEquals(static_cast<KeyTypePointer>(aKey));
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class EntryType>
|
2014-06-27 05:35:39 +04:00
|
|
|
void nsTHashtable<EntryType>::s_CopyEntry(PLDHashTable* aTable,
|
|
|
|
const PLDHashEntryHdr* aFrom,
|
|
|
|
PLDHashEntryHdr* aTo) {
|
2007-08-09 01:41:43 +04:00
|
|
|
EntryType* fromEntry =
|
2015-10-03 03:32:15 +03:00
|
|
|
const_cast<EntryType*>(static_cast<const EntryType*>(aFrom));
|
2007-08-09 01:41:43 +04:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
new (mozilla::KnownNotNull, aTo) EntryType(std::move(*fromEntry));
|
2007-08-09 01:41:43 +04:00
|
|
|
|
|
|
|
fromEntry->~EntryType();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EntryType>
|
2014-06-27 05:35:39 +04:00
|
|
|
void nsTHashtable<EntryType>::s_ClearEntry(PLDHashTable* aTable,
|
|
|
|
PLDHashEntryHdr* aEntry) {
|
2015-01-23 08:05:52 +03:00
|
|
|
static_cast<EntryType*>(aEntry)->~EntryType();
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class EntryType>
|
2015-02-11 20:46:40 +03:00
|
|
|
void nsTHashtable<EntryType>::s_InitEntry(PLDHashEntryHdr* aEntry,
|
2014-06-27 05:35:39 +04:00
|
|
|
const void* aKey) {
|
2016-08-23 01:40:10 +03:00
|
|
|
new (mozilla::KnownNotNull, aEntry)
|
|
|
|
EntryType(static_cast<KeyTypePointer>(aKey));
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
2013-09-24 05:47:30 +04:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class EntryType>
|
2013-09-24 05:47:30 +04:00
|
|
|
inline void ImplCycleCollectionUnlink(nsTHashtable<EntryType>& aField) {
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class EntryType>
|
2013-09-24 05:47:30 +04:00
|
|
|
inline void ImplCycleCollectionTraverse(
|
|
|
|
nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsTHashtable<EntryType>& aField, const char* aName, uint32_t aFlags = 0) {
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = aField.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
EntryType* entry = iter.Get();
|
|
|
|
ImplCycleCollectionTraverse(aCallback, *entry, aName, aFlags);
|
|
|
|
}
|
2013-09-24 05:47:30 +04:00
|
|
|
}
|
|
|
|
|
2016-08-23 01:45:47 +03:00
|
|
|
/**
|
|
|
|
* For nsTHashtable with pointer entries, we can have a template specialization
|
|
|
|
* that layers a typed T* interface on top of a common implementation that
|
|
|
|
* works internally with void pointers. This arrangement saves code size and
|
|
|
|
* might slightly improve performance as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need a separate entry type class for the inheritance structure of the
|
|
|
|
* nsTHashtable specialization below; nsVoidPtrHashKey is simply typedefed to a
|
|
|
|
* specialization of nsPtrHashKey, and the formulation:
|
|
|
|
*
|
2018-11-29 13:30:46 +03:00
|
|
|
* class nsTHashtable<nsPtrHashKey<T>> :
|
|
|
|
* protected nsTHashtable<nsPtrHashKey<const void>
|
2016-08-23 01:45:47 +03:00
|
|
|
*
|
|
|
|
* is not going to turn out very well, since we'd wind up with an nsTHashtable
|
|
|
|
* instantiation that is its own base class.
|
|
|
|
*/
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
class VoidPtrHashKey : public nsPtrHashKey<const void> {
|
|
|
|
typedef nsPtrHashKey<const void> Base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit VoidPtrHashKey(const void* aKey) : Base(aKey) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See the main nsTHashtable documentation for descriptions of this class's
|
|
|
|
* methods.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class nsTHashtable<nsPtrHashKey<T>>
|
|
|
|
: protected nsTHashtable<::detail::VoidPtrHashKey> {
|
|
|
|
typedef nsTHashtable<::detail::VoidPtrHashKey> Base;
|
|
|
|
typedef nsPtrHashKey<T> EntryType;
|
|
|
|
|
|
|
|
// We play games with reinterpret_cast'ing between these two classes, so
|
|
|
|
// try to ensure that playing said games is reasonable.
|
|
|
|
static_assert(sizeof(nsPtrHashKey<T>) == sizeof(::detail::VoidPtrHashKey),
|
|
|
|
"hash keys must be the same size");
|
|
|
|
|
|
|
|
nsTHashtable(const nsTHashtable& aOther) = delete;
|
|
|
|
nsTHashtable& operator=(const nsTHashtable& aOther) = delete;
|
|
|
|
|
|
|
|
public:
|
|
|
|
nsTHashtable() = default;
|
|
|
|
explicit nsTHashtable(uint32_t aInitLength) : Base(aInitLength) {}
|
|
|
|
|
|
|
|
~nsTHashtable() = default;
|
|
|
|
|
|
|
|
nsTHashtable(nsTHashtable&&) = default;
|
|
|
|
|
|
|
|
using Base::Clear;
|
|
|
|
using Base::Count;
|
2014-08-26 03:56:33 +04:00
|
|
|
using Base::GetGeneration;
|
2016-08-23 01:45:47 +03:00
|
|
|
using Base::IsEmpty;
|
|
|
|
|
|
|
|
using Base::ShallowSizeOfExcludingThis;
|
|
|
|
using Base::ShallowSizeOfIncludingThis;
|
|
|
|
using Base::MarkImmutable;
|
|
|
|
|
2017-06-17 01:06:04 +03:00
|
|
|
/* Wrapper functions */
|
2016-08-23 01:45:47 +03:00
|
|
|
EntryType* GetEntry(T* aKey) const {
|
|
|
|
return reinterpret_cast<EntryType*>(Base::GetEntry(aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Contains(T* aKey) const { return Base::Contains(aKey); }
|
|
|
|
|
|
|
|
EntryType* PutEntry(T* aKey) {
|
|
|
|
return reinterpret_cast<EntryType*>(Base::PutEntry(aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MUST_USE
|
|
|
|
EntryType* PutEntry(T* aKey, const mozilla::fallible_t&) {
|
|
|
|
return reinterpret_cast<EntryType*>(
|
|
|
|
Base::PutEntry(aKey, mozilla::fallible));
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:06:04 +03:00
|
|
|
MOZ_MUST_USE
|
|
|
|
bool EnsureInserted(T* aKey, EntryType** aEntry = nullptr) {
|
|
|
|
return Base::EnsureInserted(
|
|
|
|
aKey, reinterpret_cast<::detail::VoidPtrHashKey**>(aEntry));
|
|
|
|
}
|
|
|
|
|
2016-08-23 01:45:47 +03:00
|
|
|
void RemoveEntry(T* aKey) { Base::RemoveEntry(aKey); }
|
2017-06-17 01:06:04 +03:00
|
|
|
|
|
|
|
bool EnsureRemoved(T* aKey) { return Base::EnsureRemoved(aKey); }
|
2016-08-23 01:45:47 +03:00
|
|
|
|
|
|
|
void RemoveEntry(EntryType* aEntry) {
|
|
|
|
Base::RemoveEntry(reinterpret_cast<::detail::VoidPtrHashKey*>(aEntry));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RawRemoveEntry(EntryType* aEntry) {
|
|
|
|
Base::RawRemoveEntry(reinterpret_cast<::detail::VoidPtrHashKey*>(aEntry));
|
|
|
|
}
|
|
|
|
|
|
|
|
class Iterator : public Base::Iterator {
|
|
|
|
public:
|
|
|
|
typedef nsTHashtable::Base::Iterator Base;
|
|
|
|
|
|
|
|
explicit Iterator(nsTHashtable* aTable) : Base(aTable) {}
|
2018-05-30 22:15:35 +03:00
|
|
|
Iterator(Iterator&& aOther) : Base(std::move(aOther)) {}
|
2016-08-23 01:45:47 +03:00
|
|
|
~Iterator() = default;
|
|
|
|
|
|
|
|
EntryType* Get() const { return reinterpret_cast<EntryType*>(Base::Get()); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Iterator() = delete;
|
|
|
|
Iterator(const Iterator&) = delete;
|
|
|
|
Iterator& operator=(const Iterator&) = delete;
|
|
|
|
Iterator& operator=(Iterator&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator Iter() { return Iterator(this); }
|
|
|
|
|
|
|
|
Iterator ConstIter() const {
|
|
|
|
return Iterator(const_cast<nsTHashtable*>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwapElements(nsTHashtable& aOther) { Base::SwapElements(aOther); }
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsTHashtable_h__
|