/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef nsTHashtable_h__
#define nsTHashtable_h__
#include "nscore.h"
#include "pldhash.h"
#include "nsDebug.h"
#include NEW_H
#include "mozilla/fallible.h"
// helper function for nsTHashtable::Clear()
NS_COM_GLUE PLDHashOperator
PL_DHashStubEnumRemove(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 ordinal,
void *userArg);
/**
* 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. EntryType
must extend the following declaration,
* and must not declare any virtual functions or derive from classes
* with virtual functions. Any vtable pointer would break the
* PLDHashTable code.
*
class EntryType : public PLDHashEntryHdr * { * public: or friend nsTHashtable* * @see nsInterfaceHashtable * @see nsDataHashtable * @see nsClassHashtable * @author "Benjamin Smedberg; * // KeyType is what we use when Get()ing or Put()ing this entry * // this should either be a simple datatype (PRUint32, nsISupports*) or * // a const reference (const nsAString&) * typedef something KeyType; * // KeyTypePointer is the pointer-version of KeyType, because pldhash.h * // requires keys to cast to const void*
* typedef const something* KeyTypePointer; * * EntryType(KeyTypePointer aKey); * * // the copy constructor must be defined, even if AllowMemMove() == true * // or you will cause link errors! * EntryType(const EntryType& aEnt); * * // the destructor must be defined... or you will cause link errors! * ~EntryType(); * * // KeyEquals(): does this entry match this key? * bool KeyEquals(KeyTypePointer aKey) const; * * // KeyToPointer(): Convert KeyType to KeyTypePointer * static KeyTypePointer KeyToPointer(KeyType aKey); * * // HashKey(): calculate the hash number * static PLDHashNumber HashKey(KeyTypePointer aKey); * * // ALLOW_MEMMOVE can we move this class with memmove(), or do we have * // to use the copy constructor? * enum { ALLOW_MEMMOVE = PR_(TRUE or FALSE) }; * }
Enumerator
function for
* EnumerateEntries
* @param aEntry the entry being enumerated
* @param userArg passed unchanged from EnumerateEntries
* @return combination of flags
* @link PLDHashOperator::PL_DHASH_NEXT PL_DHASH_NEXT @endlink ,
* @link PLDHashOperator::PL_DHASH_STOP PL_DHASH_STOP @endlink ,
* @link PLDHashOperator::PL_DHASH_REMOVE PL_DHASH_REMOVE @endlink
*/
typedef PLDHashOperator (* Enumerator)(EntryType* aEntry, void* userArg);
/**
* Enumerate all the entries of the function.
* @param enumFunc the Enumerator
function to call
* @param userArg a pointer to pass to the
* Enumerator
function
* @return the number of entries actually enumerated
*/
PRUint32 EnumerateEntries(Enumerator enumFunc, void* userArg)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
s_EnumArgs args = { enumFunc, userArg };
return PL_DHashTableEnumerate(&mTable, s_EnumStub, &args);
}
/**
* remove all entries, return hashtable to "pristine" state ;)
*/
void Clear()
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashTableEnumerate(&mTable, PL_DHashStubEnumRemove, nsnull);
}
/**
* client must provide a SizeOfEntryExcludingThisFun
function for
* SizeOfExcludingThis.
* @param aEntry the entry being enumerated
* @param mallocSizeOf the function used to measure heap-allocated blocks
* @param arg passed unchanged from SizeOf{In,Ex}cludingThis
* @return summed size of the things pointed to by the entries
*/
typedef size_t (* SizeOfEntryExcludingThisFun)(EntryType* aEntry,
nsMallocSizeOfFun mallocSizeOf,
void *arg);
/**
* Measure the size of the table's entry storage, and if
* |sizeOfEntryExcludingThis| is non-NULL, measure the size of things pointed
* to by entries.
*
* @param sizeOfEntryExcludingThis the
* SizeOfEntryExcludingThisFun
function to call
* @param mallocSizeOf the function used to measure heap-allocated blocks
* @param userArg a pointer to pass to the
* SizeOfEntryExcludingThisFun
function
* @return the summed size of all the entries
*/
size_t SizeOfExcludingThis(SizeOfEntryExcludingThisFun sizeOfEntryExcludingThis,
nsMallocSizeOfFun mallocSizeOf, void *userArg = NULL) const
{
if (!IsInitialized()) {
return 0;
}
if (sizeOfEntryExcludingThis) {
s_SizeOfArgs args = { sizeOfEntryExcludingThis, userArg };
return PL_DHashTableSizeOfExcludingThis(&mTable, s_SizeOfStub, mallocSizeOf, &args);
}
return PL_DHashTableSizeOfExcludingThis(&mTable, NULL, mallocSizeOf);
}
#ifdef DEBUG
/**
* Mark the table as constant after initialization.
*
* This will prevent assertions when a read-only hash is accessed on multiple
* threads without synchronization.
*/
void MarkImmutable()
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashMarkTableImmutable(&mTable);
}
#endif
protected:
PLDHashTable mTable;
static const void* s_GetKey(PLDHashTable *table,
PLDHashEntryHdr *entry);
static PLDHashNumber s_HashKey(PLDHashTable *table,
const void *key);
static bool s_MatchEntry(PLDHashTable *table,
const PLDHashEntryHdr *entry,
const void *key);
static void s_CopyEntry(PLDHashTable *table,
const PLDHashEntryHdr *from,
PLDHashEntryHdr *to);
static void s_ClearEntry(PLDHashTable *table,
PLDHashEntryHdr *entry);
static bool s_InitEntry(PLDHashTable *table,
PLDHashEntryHdr *entry,
const void *key);
/**
* passed internally during enumeration. Allocated on the stack.
*
* @param userFunc the Enumerator function passed to
* EnumerateEntries by the client
* @param userArg the userArg passed unaltered
*/
struct s_EnumArgs
{
Enumerator userFunc;
void* userArg;
};
static PLDHashOperator s_EnumStub(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 number,
void *arg);
/**
* passed internally during sizeOf counting. Allocated on the stack.
*
* @param userFunc the SizeOfEntryExcludingThisFun passed to
* SizeOf{In,Ex}cludingThis by the client
* @param userArg the userArg passed unaltered
*/
struct s_SizeOfArgs
{
SizeOfEntryExcludingThisFun userFunc;
void* userArg;
};
static size_t s_SizeOfStub(PLDHashEntryHdr *entry,
nsMallocSizeOfFun mallocSizeOf,
void *arg);
private:
// copy constructor, not implemented
nsTHashtable(nsTHashtable